<?php
//生成邀请码
use App\Exceptions\DaoException;
use App\Helpers\Lib\RedisService;
use App\Helpers\ResponseEnum;
use App\Models\TongOrder;
use App\Models\TongUser;
use Firebase\JWT\JWT;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

function createShareCode(){
    $str  = getGuidV4();
    $share_code = substr($str, 0, 6);
    return strtoupper($share_code);
}

/**
 * @param $user_id
 * @return mixed
 */
function getToken($user_id){
    $time = time(); //当前时间
    $token = [
        'iss' => config('jwt.iss'), //签发者 可选
        'aud' => config('jwt.aud'), //接收该JWT的一方，可选
        'iat' => $time, //签发时间
        'nbf' => $time-1 , //(Not Before)：某个时间点后才能访问，比如设置time+30，表示当前时间30秒后才能使用
        'exp' => $time+config('jwt.exp'), //过期时间,这里设置2个小时
        'data' => [
            //自定义信息，不要定义敏感信息
            'userid' =>$user_id,
        ]
    ];
    return JWT::encode($token, config('jwt.key'),'HS256'); //输出Token  默认'HS256'
}

/**
 * 获取全局唯一标识符
 * @param bool $trim
 * @return string
 */
function getGuidV4($trim = true)
{
    // Windows
    if (function_exists('com_create_guid') === true) {
        $charid = com_create_guid();
        return $trim == true ? trim($charid, '{}') : $charid;
    }
    // OSX/Linux
    if (function_exists('openssl_random_pseudo_bytes') === true) {
        $data    = openssl_random_pseudo_bytes(16);
        $data[6] = chr(ord($data[6]) & 0x0f | 0x40);    // set version to 0100
        $data[8] = chr(ord($data[8]) & 0x3f | 0x80);    // set bits 6-7 to 10
        return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
    }
    // Fallback (PHP 4.2+)
    mt_srand((double)microtime() * 10000);
    $charid = strtolower(md5(uniqid(rand(), true)));
    $hyphen = chr(45);                  // "-"
    $lbrace = $trim ? "" : chr(123);    // "{"
    $rbrace = $trim ? "" : chr(125);    // "}"
    $guidv4 = $lbrace .
        substr($charid, 0, 8) . $hyphen .
        substr($charid, 8, 4) . $hyphen .
        substr($charid, 12, 4) . $hyphen .
        substr($charid, 16, 4) . $hyphen .
        substr($charid, 20, 12) .
        $rbrace;
    return $guidv4;
}

/**
 * desc:检测数据
 * email:3081089511@qq.com
 * @param array $params
 * @param string $field
 * @return bool
 */
function checkIsHasField($params = [], $field = '')
{
    return isset($params[$field]) && $params[$field] !== '' && !is_null($params[$field]);
}

//生成订单号
function makeOrderNo(): string
{
    $yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
    $orderSn =
        $yCode[intval(date('Y')) - 2017] . strtoupper(dechex(date('m'))) . date(
            'd') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf(
            '%02d', rand(0, 99));
    return $orderSn;
}



/**
 * 用递归获根据id获取所有的上级
 * @param $cate
 * @param $id
 * @return array
 */
function get_top_pid($cate, $id)
{
    $arr = [];
    foreach ($cate as $v) {
        if ($v['id'] == $id) {
            if ($v['p_id'] != 0) {
                $arr[] = $v['p_id'];
                $arr   = array_merge(get_top_pid($cate, $v['p_id']), $arr);
                //var_dump($arr);
            }
        }
    }
    return $arr;
}


/**
 * 获取用户的各种收益
 *
 * @return float[]
 */
function get_user_profit($u_id){

    //  获取今日收益 (当天时间)
    $today_start = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
    $today_end   = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;

    //本周的时间
    $start_date = date('Y-m-d H:i:s',mktime(0,0,0,date('m'),date('d')-date('w')+1,date('Y')));
    $end_date = date('Y-m-d H:i:s',mktime(23,59,59,date('m'),date('d')-date('w')+7,date('Y')));

    //  TODO 获取本月预估
    $month_start = date('Y-m-01', strtotime(date("Y-m-d")));
    $month_end   = date('Y-m-d', strtotime("$month_start +1 month -1 day"));

    //  获取上月收益 (上个月时间)
    $last_month_start = date('Y-m-01', strtotime('-1 month'));
    $last_month_end   = date('Y-m-t', strtotime('-1 month'));

    return [
        'today_money'      => 0.00,
        'week_money'       =>0.00,
        'month_money'      => 0.00,
        'last_month_money' => 0.00,
    ];
}

/**
 * @param $user_id
 * @return array
 */
function  getUserShareCount($user_id)
{
    //  获取今日收益 (当天时间)
    $today_start = date('Y-m-d H:i:s',mktime(0, 0, 0, date('m'), date('d'), date('Y')));
    $today_end   = date('Y-m-d H:i:s',mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1);

    $month_start = date('Y-m-01 H:i:s', strtotime(date("Y-m-d")));
    $month_end   = date('Y-m-d', strtotime("$month_start +1 month -1 day")).' 23:59:59';

    $res_data = [
        'today'=>TongUser::query()->where('p_id',$user_id)->whereBetween('created_at',[$today_start,$today_end])->count(),
        'month'=>TongUser::query()->where('p_id',$user_id)->whereBetween('created_at',[$month_start,$month_end])->count(),
        'all'=>TongUser::query()->where('p_id',$user_id)->count()
    ];
    return $res_data;

    //dd($res_data);
}



/**
 * 获取下级用户的订单数和订单的金额
 * @param $u_id
 * @return array
 */
function get_child_order_count($u_id){
    $userInfo = TongUser::query()->where('id',$u_id)->first();
    $order_count = \App\Models\TongOrder::query()->whereIn('u_id',explode(',',$userInfo['child_id']))->count();
    $order_price = \App\Models\TongOrder::query()->whereIn('u_id',explode(',',$userInfo['child_id']))->sum('order_price');
    return [
        'order_count'=>$order_count,
        'order_price'=>$order_price
    ];
}


/**
 * 重置用户的关系
 * @return void
 */
function reset_child_user(){

    TongUser::query()->update(['child_id'=>'']);
    $user_list = TongUser::query()->get(['id','p_id','child_id','identity_id','is_real_store'])->toArray();
    //清空掉redis的集合
    TongUser::query()->chunk(1000,function($user) use($user_list){
        //var_dump($user);
        foreach ($user as $item){
            $user_id = $item['id'];
            $parents   = get_top_pid($user_list, $user_id);

                foreach ($parents as $m_id) {
                    $ids      = [];
                    $child_id = TongUser::query()->where('id', $m_id)->value('child_id');
                    if (empty($child_id)) {
                        $ids[] = $user_id;
                    } else {
                        $ids   = explode(',', $child_id);
                        $ids[] = $user_id;
                    }
                    $ids = implode(',', $ids);
                    TongUser::query()->where('id', $m_id)->update(['child_id' => $ids]);
                }


            echo '会员'.$user_id.'关系重置完成</br>';
        }
    });
}




/**
 * 酱用户的关系存储在缓存中方便快速获取
 * @return void
 */
function addRedisUserData_old($data){
    $key = config('user.add_redis_data_key');
    $res = Cache::store('redis')->get($key);
    if($res){
        array_push($res,$data);
    }else{
        $res[] = $data;
    }
    Cache::store('redis')->put($key,$res);
}

/**
 * @param $data
 * @return void
 * @throws \Psr\SimpleCache\InvalidArgumentException
 */
function addRedisUserData($data){
    \App\Helpers\Lib\RedisService::hUpdate($data['id'],$data);
}



/**
 * 获取缓存的用户关系
 * @return mixed
 * @throws \Psr\SimpleCache\InvalidArgumentException
 */
function getRedisUserData_old(){
    $key = config('user.add_redis_data_key');
    $result = Cache::store('redis')->get($key);
    if(is_null($result)){
        $result = resetRedisData();
    }
    return $result;
}

/**
 * @return array|mixed
 * @throws \Psr\SimpleCache\InvalidArgumentException
 */
function getRedisUserData(){
    $result = \App\Helpers\Lib\RedisService::getAlls();
    if(empty($result)){
        $result = resetRedisData();
    }
    return $result;
}

/**
 * 重置redis用户关系(这个是老的原来是存储再)
 * @return array
 */
function resetRedisData_old(){
    $user_data = TongUser::query()
        ->orderBy('id')
        ->get(['id','p_id','identity_id','is_real_store'])->toArray();
    $key = config('user.add_redis_data_key');
    Cache::store('redis')->put($key,$user_data);
    return $user_data;
}

/**
 * @return array
 */
function resetRedisData(){
    //清空掉之前的
    \App\Helpers\Lib\RedisService::delAll();
    $user_data = TongUser::query()
        ->orderBy('id')
        ->get(['id','p_id','identity_id','is_real_store'])->toArray();
    foreach ($user_data as $k=>$v){
        \App\Helpers\Lib\RedisService::hSet($v['id'],$v);
    }
    return $user_data;
}


/**
 * 获取顶级用户
 * @param $list
 * @param $id
 * @return mixed
 */
function getParent($list,$id){
    foreach($list as $item) if($id == $item['id']) $citem = $item; // 当前
    if(!$citem['p_id']) return $citem; // 它没有上级，则它本身就是最上级
    return getParent($list,$citem['p_id']);
}

/**
 * 获取下级
 * @param $list
 * @param $id
 * @return array
 */
function getChilds($list,$id){
    $childs = [];
    foreach($list as $item){
        if($id == $item['p_id']){
            $childs[] = $item;
            $childs = array_merge($childs,getChilds($list,$item['id']));
        }
    }
    return $childs;
}

/**
 * 获取指定级别的上级
 * @param $cate
 * @param $id
 * @param $level
 * @param $i
 * @return array
 */
function getParentLevel($cate, $id, $level = 0, $i = 0)
{
    $arr = [];
    foreach ($cate as $v) {
        if ($i < $level) {
            if ($v['id'] == $id) {
                $arr[] = $v;
                $i++;
                $arr = array_merge(getParentLevel($cate, $v['p_id'], $level, $i), $arr);
            }
        }
    }
    return $arr;
}

function getChildLevel($members, $mid, $level = 0)
{
    $arr = [];
    foreach ($members as $key => $v) {
        if ($v['p_id'] == $mid) {  //pid为0的是顶级分类
            $v['level'] = $level + 1;
            $arr[]      = $v;
            $arr        = array_merge($arr, getChildLevel($members, $v['id'], $level + 1));
        }
    }
    return $arr;
}

/**
 * 获取下下面的直推用户
 * @param $u_id
 * @return mixed
 * @throws \Psr\SimpleCache\InvalidArgumentException
 */
function getZhiUser($u_id)
{
    $user_list = getRedisUserData();
    return array_filter($user_list,function($val) use($u_id){
        return $val['p_id']==$u_id;
    });

}

/**
 * 生成测试用户
 * @return void
 */
function createMoreUser($num,$p_id,$identity_id=""){
    $pInfo = TongUser::query()->where('id',$p_id)->first();
    if(!$pInfo){
        echo 'pid错误';
        return false;
    }
    if(empty($identity_id)){
        $identity_id = $pInfo['identity_id'];
    }
    //$user_data = [];
    for ($i=0;$i<$num;$i++){
        $mobile = generateRandomPhoneNumber();
        $user_data= [
            'nickname'=>$pInfo['nickname'].'直推'.$i,
            'mobile'=>$mobile,
            'share_code'=>createShareCode(),
            'p_id'=>$pInfo['id'],
            'three_child_id'=>'',
            'identity_id'=>$identity_id,//用户的等级
            'account'=>$mobile.'123',
            'password'=>'123456',
            'created_at'=>date('Y-m-d H:i:s',time()),
            'updated_at'=>date('Y-m-d H:i:s',time()),
        ];
        $user_id = TongUser::query()->insertGetId($user_data);
       // var_dump($user_id);
        //插入用户的收货地址
        $user_address_data = [
            'u_id'=>$user_id,
            'province_id'=>'1',
            'province_val'=>'河南省',
            'city_id'=>'35',
            'city_val'=>'洛阳市',
            'area_id'=>'430',
            'area_val'=>'新安县',
            'address'=>'*****小区几号楼',
            'created_at'=>date('Y-m-d H:i:s',time()),
            'updated_at'=>date('Y-m-d H:i:s',time()),
        ];
        \App\Models\TongUserAddress::query()->insert($user_address_data);
    }
    //restUser();
}

/**
 * 生成用户直推订单
 * @return void
 */
function createMoreOrder($user_id,$goods_id){
   // resetRedisData();
    //获取商品信息
    $goodsInfo = \App\Models\TongGood::query()->where('id',$goods_id)->first()->toArray();
    //获取直推用户
    $zhi_user = getZhiUser($user_id);
    //var_dump($zhi_user);
    if(count($zhi_user)<0){
        return '此用户没有直推人';
    }
    foreach ($zhi_user as $val){
       // var_dump($val['id']);
        $address = \App\Models\TongUserAddress::query()->where('u_id',$val['id'])->first();
        $user_order_data = [
            'order_no'=>makeOrderNo(),
            'u_id'=>$val['id'],
            'goods_num'=>1,
            'goods_id'=>$goods_id,
            'order_price'=>$goodsInfo['price'],
            'all_order_price'=>$goodsInfo['price'],
            'order_goods_info'=>json_encode($goodsInfo),
            'order_address_info'=>json_encode($address),
            'goods_price'=>$goodsInfo['price'], //购买商品的价格
            'status'=>2,//支付成功
            'pay_status'=>1,
            'pay_time'=>date('Y-m-d h:i:s',time()),
            'address_id'=>$address['id'],
            'created_at'=>date('Y-m-d H:i:s',time()),
            'updated_at'=>date('Y-m-d H:i:s',time()),
        ];
        TongOrder::query()->insert($user_order_data);
    }
}

/**
 * 生成模拟使用的手机号
 * @return string
 */
function generateRandomPhoneNumber() {
    $prefix = array("13", "14", "15", "16", "17", "18", "19");
    $randomPrefix = $prefix[array_rand($prefix)];
    $randomNumber = mt_rand(1000, 9999) . mt_rand(1000, 9999);
    return $randomPrefix . $randomNumber;
}

/**
 * 用户升级 用户下单成功时调用
 * @return true|void
 * @throws \Psr\SimpleCache\InvalidArgumentException
 */
function userUpgrade($u_id){
    \Illuminate\Support\Facades\Log::info('用户业绩升级',['u_id'=>$u_id]);
    //$u_id = 42;
    //获取用户信息
    $user_info = TongUser::query()->where('id',$u_id)->first();

    $new_user_info = RedisService::hGet($user_info['id']);

    if($new_user_info['identity_id']==0){
        return true;
    }



}


/**
 * 统计用户的升级标准
 * @param $u_id
 * @return int[]
 * @throws \Psr\SimpleCache\InvalidArgumentException
 */
function countUserUpgrade($u_id){
    $res_data = [
        'zhi_num'=>0,
        'order_num'=>0,
    ];
    \Illuminate\Support\Facades\Log::info('用户业绩升级',['u_id'=>$u_id]);
    //$u_id = 42;
    //获取用户信息
    $user_info = TongUser::query()->where('id',$u_id)->first();

    if($user_info['identity_id']==0){
        \Illuminate\Support\Facades\Log::info('用户业绩升级,当前用户身份为普通用户不往下继续进行',['u_id'=>$u_id,'identity_id'=>$user_info['identity_id']]);
        return $res_data;
    }

    $user_list = getRedisUserData();


            $user_info = TongUser::query()->where('id',$u_id)->first();
            $zhi_user = getZhiUser($u_id);
            \Illuminate\Support\Facades\Log::info('直推用户数据',$zhi_user);
            $child_user = getChildLevel($user_list,$u_id);
            $zhi_user_ids = array_column($zhi_user,'id');
            $child_user_ids = array_column($child_user,'id');
            \Illuminate\Support\Facades\Log::info('用户'.$u_id.'业绩升级',[$zhi_user_ids,$child_user_ids]);
            if(config('user.order_type')==0){
                $status_arr = [1,3,4,5];
            }else{
                $status_arr = [5];
            }
            if($user_info['identity_id']<3){
                \Illuminate\Support\Facades\Log::info('identity_id<3');
                $orderQuery = TongOrder::query()->whereIn('u_id',$child_user_ids);
                \Illuminate\Support\Facades\Log::info('直推id',$zhi_user_ids);
                $res_data['zhi_num'] = TongUser::query()->whereIn('id',$zhi_user_ids)->count();
                $res_data['order_num'] = $orderQuery->whereIn('status',$status_arr)->count();
            }else{
                if($user_info['identity_id']==3){
                    \Illuminate\Support\Facades\Log::info('identity_id==3');
                    $zhi_num = TongUser::query()->whereIn('id',$zhi_user_ids)
                        ->where('identity_id','>=',3)->count();
                    $orderQuery = TongOrder::query()->whereIn('u_id',$child_user_ids);
                    if(config('user.order_type')==0){
                        $status_arr = [1,3,4,5];
                    }else{
                        $status_arr = [5];
                    }
                    $order_num = $orderQuery->whereIn('status',$status_arr)->count();
                    //直推10个体验店 或
                    $flag = false;
                    //查询直推用户下面的体验点
                    $zhi_user_info = TongUser::query()->whereIn('id',$zhi_user_ids)
                        ->where('identity_id','>=',3)->get();
                    $shichang = []; //用于存储达标的市场
                    $shichang_num = 0;
                    $shichang_user_num = 0;
                    if($zhi_user_info->isNotEmpty()){
                        $zhi_user_info_arr = $zhi_user_info->toArray();
                        //$shichang_user_num = 0;
                        //$shichang_num = 0;
                        foreach ($zhi_user_info_arr as $k=>$v){
                            if(config('user.upgrade.three.shichang.is_all')==false){
                                // echo 'a';
                                $child_res = getZhiUser($v['id']);
                            }else{
                                //  echo 'b';
                                $child_res = getChildLevel($user_list,$v['id']);

                            }
                            //var_dump($v['id']);
                            // var_dump($child_res);
                            $child_res = array_column($child_res,'id');
                            // var_dump($child_res);
                            if(config('user.upgrade.three.shichang.is_self')==true){
                                array_push($child_res,$v['id']);
                            }
                            //var_dump($child_res);
                            $count = TongUser::query()->whereIn('p_id',$child_res)->where('identity_id',3)->count();
                            $shichang[] =['id',$v['id'],'count'=>$count];
                            if($count>0){
                                $shichang_num = $shichang_num+1;
                                $shichang_user_num = $shichang_user_num+$count;
                            }
                        }

                    }
                    $res_data['order_num']=$shichang_num;
                    $res_data['zhi_num'] = $shichang_user_num;

                }



                if($user_info['identity_id']==4){
                    \Illuminate\Support\Facades\Log::info('identity_id==3');
                    $zhi_num = TongUser::query()->whereIn('id',$zhi_user_ids)
                        ->where('identity_id','>=',4)->count();
                    $orderQuery = TongOrder::query()->whereIn('u_id',$child_user_ids);
                    if(config('user.order_type')==0){
                        $status_arr = [1,3,4,5];
                    }else{
                        $status_arr = [5];
                    }
                    $order_num = $orderQuery->whereIn('status',$status_arr)->count();
                    //直推10个体验店 或
                    $flag = false;
                    //查询直推用户下面的体验点
                    $zhi_user_info = TongUser::query()->whereIn('id',$zhi_user_ids)
                        ->where('identity_id','>=',4)->get();
                    $shichang = []; //用于存储达标的市场
                    if($zhi_user_info->isNotEmpty()){
                        $zhi_user_info_arr = $zhi_user_info->toArray();
                        $shichang_user_num = 0;
                        $shichang_num = 0;
                        foreach ($zhi_user_info_arr as $k=>$v){
                            if(config('user.upgrade.four.shichang.is_all')==false){
                                // echo 'a';
                                $child_res = getZhiUser($v['id']);
                            }else{
                                //  echo 'b';
                                $child_res = getChildLevel($user_list,$v['id']);

                            }
                            //var_dump($v['id']);
                            // var_dump($child_res);
                            $child_res = array_column($child_res,'id');
                            // var_dump($child_res);
                            if(config('user.upgrade.four.shichang.is_self')==true){
                                array_push($child_res,$v['id']);
                            }
                            //var_dump($child_res);
                            $count = TongUser::query()->whereIn('p_id',$child_res)->where('identity_id',3)->count();
                            $shichang[] =['id',$v['id'],'count'=>$count];
                            if($count>0){
                                $shichang_num = $shichang_num+1;
                                $shichang_user_num = $shichang_user_num+$count;
                            }
                        }
                        if($shichang_num>=3){
                            $flag = true;
                        }

                    }
                    $res_data['order_num']=$shichang_num;
                    $res_data['zhi_num'] = $shichang_user_num;
                }


            }


            return $res_data;


}

/**
 * 获取今天的销售额
 * @return int[]
 */
function getTodayOrderMoney()
{
    $all_money_arr = [
        'all_money'=>0,//今日订单总金额
        'return_money'=>0,//昨日退单金额
    ];
    //今日开始时间
    $today_start = date("Y-m-d 00:00:00");
    //今日结束时间
    $today_end = date("Y-m-d 23:59:59");

    //昨日开始时间
    $yest_day_start = date("Y-m-d 00:00:00",strtotime("-1 day"));
    //昨日结束时间
    $yest_day_end = date("Y-m-d 23:59:59",strtotime("-1 day"));

    //return $all_money;
    $all_money = TongOrder::query()->whereBetween('pay_time',[$today_start,$today_end])
        ->whereNotIn('status',[6,7])->sum('all_order_price');
    $yest_return_money = TongOrder::query()->whereBetween('return_time',[$yest_day_start,$yest_day_end])
        ->whereIn('status',[6,7])->sum('all_order_price');

    $all_money_arr['all_money'] = $all_money;
    $all_money_arr['return_money'] = $yest_return_money;
    return $all_money_arr;

}

/**
 * 查询公司的团队
 * @return
 */
function getCompanyTeam()
{

    $user_list = getRedisUserData();
    //获取到身份位公司的人
    $user_list_res = array_filter($user_list,function($values){
        //var_dump($values);
        if($values['identity_id']==4){
            return $values;
        }
    });
    $user_list_res = array_values($user_list_res);
    if(count($user_list_res)==0){
        \Illuminate\Support\Facades\Log::info('公司津贴-没有公司身份的人存在');
        return false;
    }
    $day_money = getTodayOrderMoney();
    //分润金额
    $all_order_money = bcsub($day_money['all_money'],$day_money['return_money'],2);
    \Illuminate\Support\Facades\Log::info('公司津贴-',[$all_order_money]);
    if($all_order_money<=0){
        return false;
    }
    $all_order_money = bcdiv(bcmul($all_order_money,config('user.team.percentage'),2),100,2);

    if(config('user.partner.is_debug')){
        $all_order_money = 1000;
    }
    //dd($user_list_res);
    foreach ($user_list_res as $k=>&$v){
        $child_list = getChilds($user_list,$v['id']);
        $child_list_arr = array_filter($child_list,function($child_val){
            if(($child_val['identity_id']<4) && ($child_val['identity_id']>=1)){
                return $child_val;
            }
        });
        if(config('user.team.is_self')){
           // var_dump($v);
            array_push($child_list_arr,$v);
        }
        $v['child_user'] = $child_list_arr;
        $team_order_num = 0; //统计的订单数量
        $order_order_money = 0;//一共完成的金额
        $user_profit_arr = []; //记录用户的团队收益
        $child_list_number = count($child_list_arr);//团队人数
        echo '团队'.$k.'人数'.$child_list_number;
        foreach ($child_list_arr as $ck=>$cv){
            //查询用户的订单
            $team_order_num+=TongOrder::query()->where('id',$cv['id'])
                ->whereIn('status',[1,3,4,5])->count();
            $order_price = TongOrder::query()->where('id',$cv['id'])
                ->whereIn('status',[1,3,4,5])->sum('all_order_price');
            $order_order_money =bcadd($order_price,$order_order_money,2);
            //var_dump($cv);
           // exit();
            if(config('user.partner.is_debug')){
                $order_price = 100;
            }

            if($order_price>=config('user.team.fenrun.v1')[0][0] && $order_price<=config('user.team.fenrun.v1')[0][1]){
                echo 'team_id'.$v['id'].'走的398业绩';
                $money = bcdiv(bcdiv(bcmul($all_order_money,config('user.team.fenrun.v1')[1],2),100,2),$child_list_number,2);
                $user_profit_arr[] = [
                    'u_id'=>$cv['id'],
                    'team_id'=>$v['id'],
                    'money'=>$money, //先写死测试下
                    'type'=>4,
                    'symbol'=>1,
                    'status'=>2,
                    'remark'=>'公司团队收益'.$money.'元',
                    'created_at'=>date('Y-m-d H:i:s',time()),
                    'updated_at'=>date('Y-m-d H:i:s',time())
                ];
            }
            if($order_price>=config('user.team.fenrun.v2')[0][0] && $order_price<=config('user.team.fenrun.v2')[0][1]){
                $money = bcdiv(bcdiv(bcmul($all_order_money,config('user.team.fenrun.v2')[1],2),100,2),$child_list_number,2);
                $user_profit_arr[] = [
                    'u_id'=>$cv['id'],
                    'team_id'=>$v['id'],
                    'money'=>$money, //先写死测试下
                    'type'=>4,
                    'symbol'=>1,
                    'status'=>2,
                    'remark'=>'公司团队收益'.$money.'元',
                    'created_at'=>date('Y-m-d H:i:s',time()),
                    'updated_at'=>date('Y-m-d H:i:s',time())
                ];
            }
            if($order_price>=config('user.team.fenrun.v3')[0][0] && $order_price<=config('user.team.fenrun.v3')[0][1]){
                $money = bcdiv(bcdiv(bcmul($all_order_money,config('user.team.fenrun.v3')[1],2),100,2),$child_list_number,2);
                $user_profit_arr[] = [
                    'u_id'=>$cv['id'],
                    'team_id'=>$v['id'],
                    'money'=>$money, //先写死测试下
                    'type'=>4,
                    'symbol'=>1,
                    'status'=>2,
                    'remark'=>'公司团队收益'.$money.'元',
                    'created_at'=>date('Y-m-d H:i:s',time()),
                    'updated_at'=>date('Y-m-d H:i:s',time())
                ];
            }
            if($order_price>=config('user.team.fenrun.v4')[0][0] && $order_price<=config('user.team.fenrun.v4')[0][1]){
                $money = bcdiv(bcdiv(bcmul($all_order_money,config('user.team.fenrun.v4')[1],2),100,2),$child_list_number,2);
                $user_profit_arr[] = [
                    'u_id'=>$cv['id'],
                    'team_id'=>$v['id'],
                    'money'=>$money, //先写死测试下
                    'type'=>4,
                    'symbol'=>1,
                    'status'=>2,
                    'remark'=>'公司团队收益'.$money.'元',
                    'created_at'=>date('Y-m-d H:i:s',time()),
                    'updated_at'=>date('Y-m-d H:i:s',time())
                ];
            }
            if($order_price>=config('user.team.fenrun.v5')[0][0] && $order_price<=config('user.team.fenrun.v5')[0][1]){
                $money = bcdiv(bcdiv(bcmul($all_order_money,config('user.team.fenrun.v5')[1],2),100,2),$child_list_number,2);
                $user_profit_arr[] = [
                    'u_id'=>$cv['id'],
                    'team_id'=>$v['id'],
                    'money'=>$money, //先写死测试下
                    'type'=>4,
                    'symbol'=>1,
                    'status'=>2,
                    'remark'=>'公司团队收益'.$money.'元',
                    'created_at'=>date('Y-m-d H:i:s',time()),
                    'updated_at'=>date('Y-m-d H:i:s',time())
                ];
            }
            if($order_price>=config('user.team.fenrun.v6')[0][0] && $order_price<=config('user.team.fenrun.v6')[0][1]){
                $money = bcdiv(bcdiv(bcmul($all_order_money,config('user.team.fenrun.v6')[1],2),100,2),$child_list_number,2);
                $user_profit_arr[] = [
                    'u_id'=>$cv['id'],
                    'team_id'=>$v['id'],
                    'money'=>$money, //先写死测试下
                    'type'=>4,
                    'symbol'=>1,
                    'status'=>2,
                    'remark'=>'公司团队收益',
                    'created_at'=>date('Y-m-d H:i:s',time()),
                    'updated_at'=>date('Y-m-d H:i:s',time())
                ];
            }
            if($order_price>=config('user.team.fenrun.v7')[0][0] && $order_price<=config('user.team.fenrun.v7')[0][1]){
                $money = bcdiv(bcdiv(bcmul($all_order_money,config('user.team.fenrun.v7')[1],2),100,2),$child_list_number,2);
                $user_profit_arr[] = [
                    'u_id'=>$cv['id'],
                    'team_id'=>$v['id'],
                    'money'=>$money, //先写死测试下
                    'type'=>4,
                    'symbol'=>1,
                    'status'=>2,
                    'remark'=>'公司团队收益'.$money.'元',
                    'created_at'=>date('Y-m-d H:i:s',time()),
                    'updated_at'=>date('Y-m-d H:i:s',time())
                ];
            }
        }
        $v['child_user'] = $child_list_arr;
    }
    //dd($user_profit_arr);
    \App\Models\TongUserProfit::query()->insert($user_profit_arr);
}

/**
 * @return false|void
 * @throws \Psr\SimpleCache\InvalidArgumentException
 */
function getPartnerTeam(){

    $user_list = getRedisUserData();
    //获取到身份位公司的人
    $user_list_res = array_filter($user_list,function($values){
        //var_dump($values);
        if($values['identity_id']==5){
            return $values;
        }
    });
    $user_list_res = array_values($user_list_res);
    if(count($user_list_res)==0){
        return false;
    }
    //var_dump($user_list_res);
    $day_money = getTodayOrderMoney();
    //分润金额
    $all_order_money = bcsub($day_money['all_money'],$day_money['return_money'],2);
    if($all_order_money<=0){
        return false;
    }
    $all_order_money = bcdiv(bcmul($all_order_money,config('user.partner.percentage'),2),100,2);

    if(config('user.partner.is_debug')){
        $all_order_money = 1000;
    }


    foreach ($user_list_res as $k=>&$v){
        $child_list = getChilds($user_list,$v['id']);

        $child_list_arr = array_filter($child_list,function($child_val){
            if(($child_val['identity_id']<5) && ($child_val['identity_id']>=1)){
                return $child_val;
            }
        });
        if(config('user.partner.is_self')){
            // var_dump($v);
            array_push($child_list_arr,$v);
        }
        $v['child_user'] = $child_list_arr;
        $team_order_num = 0; //统计的订单数量
        $order_order_money = 0;//一共完成的金额
        $user_profit_arr = []; //记录用户的团队收益
        $child_list_number = count($child_list_arr);//团队人数
        foreach ($child_list_arr as $ck=>$cv){
            //查询用户的订单
            $team_order_num+=TongOrder::query()->where('id',$cv['id'])
                ->whereIn('status',[1,3,4,5])->count();
            $order_price = TongOrder::query()->where('id',$cv['id'])
                ->whereIn('status',[1,3,4,5])->sum('all_order_price');
            $order_order_money =bcadd($order_price,$order_order_money,2);
            //var_dump($cv);
            if(config('user.partner.is_debug')){
                $order_price =159;
            }

            // exit();
            if($order_price>=config('user.partner.fenrun.v1')[0][0] && $order_price<=config('user.partner.fenrun.v1')[0][1]){
                $money = bcdiv(bcdiv(bcmul($all_order_money,config('user.partner.fenrun.v1')[1],2),100,2),$child_list_number,2);
                $user_profit_arr[] = [
                    'u_id'=>$cv['id'],
                    'team_id'=>$v['id'],
                    'money'=>$money, //先写死测试下
                    'type'=>5,
                    'symbol'=>1,
                    'status'=>2,
                    'remark'=>'合伙人团队收益'.$money.'元',
                    'created_at'=>date('Y-m-d H:i:s',time()),
                    'updated_at'=>date('Y-m-d H:i:s',time())
                ];
            }
            if($order_price>=config('user.partner.fenrun.v2')[0][0] && $order_price<=config('user.partner.fenrun.v2')[0][1]){
                $money = bcdiv(bcdiv(bcmul($all_order_money,config('user.partner.fenrun.v2')[1],2),100,2),$child_list_number,2);
                $user_profit_arr[] = [
                    'u_id'=>$cv['id'],
                    'team_id'=>$v['id'],
                    'money'=>$money, //先写死测试下
                    'type'=>5,
                    'symbol'=>1,
                    'status'=>2,
                    'remark'=>'合伙人团队收益'.$money.'元',
                    'created_at'=>date('Y-m-d H:i:s',time()),
                    'updated_at'=>date('Y-m-d H:i:s',time())
                ];
            }
            if($order_price>=config('user.partner.fenrun.v3')[0][0] && $order_price<=config('user.partner.fenrun.v3')[0][1]){
                $money = bcdiv(bcdiv(bcmul($all_order_money,config('user.partner.fenrun.v3')[1],2),100,2),$child_list_number,2);
                $user_profit_arr[] = [
                    'u_id'=>$cv['id'],
                    'team_id'=>$v['id'],
                    'money'=>$money, //先写死测试下
                    'type'=>5,
                    'symbol'=>1,
                    'status'=>2,
                    'remark'=>'合伙人团队收益'.$money.'元',
                    'created_at'=>date('Y-m-d H:i:s',time()),
                    'updated_at'=>date('Y-m-d H:i:s',time())
                ];
            }
            if($order_price>=config('user.partner.fenrun.v4')[0][0] && $order_price<=config('user.partner.fenrun.v4')[0][1]){
                $money = bcdiv(bcdiv(bcmul($all_order_money,config('user.partner.fenrun.v4')[1],2),100,2),$child_list_number,2);
                $user_profit_arr[] = [
                    'u_id'=>$cv['id'],
                    'team_id'=>$v['id'],
                    'money'=>$money, //先写死测试下
                    'type'=>5,
                    'symbol'=>1,
                    'status'=>2,
                    'remark'=>'合伙人团队收益'.$money.'元',
                    'created_at'=>date('Y-m-d H:i:s',time()),
                    'updated_at'=>date('Y-m-d H:i:s',time())
                ];
            }
            if($order_price>=config('user.partner.fenrun.v5')[0][0] && $order_price<=config('user.partner.fenrun.v5')[0][1]){
                $money = bcdiv(bcdiv(bcmul($all_order_money,config('user.partner.fenrun.v5')[1],2),100,2),$child_list_number,2);
                $user_profit_arr[] = [
                    'u_id'=>$cv['id'],
                    'team_id'=>$v['id'],
                    'money'=>$money, //先写死测试下
                    'type'=>5,
                    'symbol'=>1,
                    'status'=>2,
                    'remark'=>'合伙人团队收益'.$money.'元',
                    'created_at'=>date('Y-m-d H:i:s',time()),
                    'updated_at'=>date('Y-m-d H:i:s',time())
                ];
            }
            if($order_price>=config('user.partner.fenrun.v6')[0][0] && $order_price<=config('user.partner.fenrun.v6')[0][1]){
                $money = bcdiv(bcdiv(bcmul($all_order_money,config('user.partner.fenrun.v6')[1],2),100,2),$child_list_number,2);
                $user_profit_arr[] = [
                    'u_id'=>$cv['id'],
                    'team_id'=>$v['id'],
                    'money'=>$money, //先写死测试下
                    'type'=>5,
                    'symbol'=>1,
                    'status'=>2,
                    'remark'=>'合伙人团队收益'.$money.'元',
                    'created_at'=>date('Y-m-d H:i:s',time()),
                    'updated_at'=>date('Y-m-d H:i:s',time())
                ];
            }
            if($order_price>=config('user.partner.fenrun.v7')[0][0] && $order_price<=config('user.partner.fenrun.v7')[0][1]){
                $money = bcdiv(bcdiv(bcmul($all_order_money,config('user.partner.fenrun.v7')[1],2),100,2),$child_list_number,2);
                $user_profit_arr[] = [
                    'u_id'=>$cv['id'],
                    'team_id'=>$v['id'],
                    'money'=>$money, //先写死测试下
                    'type'=>5,
                    'symbol'=>1,
                    'status'=>2,
                    'remark'=>'合伙人团队收益'.$money.'元',
                    'created_at'=>date('Y-m-d H:i:s',time()),
                    'updated_at'=>date('Y-m-d H:i:s',time())
                ];
            }
        }
        $v['child_user'] = $child_list_arr;
    }
    \App\Models\TongUserProfit::query()->insert($user_profit_arr);
}
/**
 * 重置用户的关系
 * @return void
 */
function restUser(){
      reset_child_user();
      resetRedisData();
}


/**
 * @param $employees
 * @param $employeeId
 * @param $superiors
 * @return array|mixed
 */
//function getAllParents($employees, $employeeId) {
//    static $superiors = [];
//    // 获取指定ID的员工的上级
//    $superior = array_filter($employees, function ($employee) use ($employeeId) {
//        return $employee['id'] == $employeeId;
//    });
//    $superior = array_pop($superior); // 获取上级，假设每个ID唯一
//
//    if ($superior) {
//        // 将上级添加到结果数组
//        $superiors[] = $superior;
//
//        // 递归查找上级的上级，直到没有上级为止
//        getAllParents($employees, $superior['p_id']);
//    }
//    //$return_arr = $superior;
//    //$superior = [];
//    //return $return_arr;
//    // 返回所有上级
//    return $superiors;
//}

function getAllParents($employees, $employeeId, &$superiors = []) {
    // 获取指定ID的员工的上级
    $superior = array_filter($employees, function ($employee) use ($employeeId) {
        return $employee['id'] == $employeeId;
    });
    $superior = array_pop($superior);

    // 获取上级，假设每个ID唯一
    if ($superior) {
        // 将上级添加到结果数组
        $superiors[] = $superior;
        // 递归查找上级的上级，直到没有上级为止
        getAllParents($employees, $superior['p_id'], $superiors);
    }

    // 返回所有上级
    return $superiors;
}


/**
 * 复购津贴
 * @return false
 */
function repurchase_money($order_id){
    \Illuminate\Support\Facades\Log::info('执行复购津贴1111111111111');
    //获取订单详情
    $order_info = TongOrder::query()->where('id',$order_id)->first();
    //获取用户详情
    $user_info = TongUser::query()->where('id',$order_info['u_id'])->first();
    if($user_info['identity_id']==0){
        \Illuminate\Support\Facades\Log::info('复购津贴-'.$user_info['nickname'].'用户等级0');
        return false;
    }
//    $is_first_order = TongOrder::query()->where('u_id',$order_info['u_id'])
//        ->where('goods_id',$order_info['goods_id'])
//        ->whereIn('status',[1,3,4,5])->count();
//    if($is_first_order<=1 && $order_info['is_shop_goods']==0){
//        \Illuminate\Support\Facades\Log::info('复购津贴-'.$user_info['nickname'].'首次订单没有复购奖');
//        return false;
//    }

    if(in_array($order_info['goods_id'],[17]) && $user_info['identity_id']==1){
        $is_first_order = TongOrder::query()->where('u_id',$order_info['u_id'])
            ->where('goods_id',$order_info['goods_id'])
            ->whereIn('status',[1,3,4,5])->count();
        if($is_first_order<=1 && $order_info['is_shop_goods']==0){
            \Illuminate\Support\Facades\Log::info('复购津贴-'.$user_info['nickname'].'首次订单没有复购奖');
            return false;
        }
    }

    $user_list = getRedisUserData();

    $all_parents = getAllParents($user_list, $user_info['id']);

    $temp_arr_parents = [];
    if(!empty($all_parents)){
        //数组去除重复
        foreach ($all_parents as $rv){
            $temp_arr_parents[$rv['id']] = $rv;
        }
        $all_parents = array_values($temp_arr_parents);
    }


    unset($all_parents[0]);
    $all_parents = array_values($all_parents);

    //过滤掉普通会员
    $all_parents = array_filter($all_parents,function($parents_val) use ($user_info){
        return $parents_val['identity_id']>1 && ($parents_val['identity_id']>$user_info['identity_id']);
    });

    //var_dump($all_parents);
    //var_dump($order_info['order_goods_info']);exit();
    $goods_info = $order_info['order_goods_info'];

    if($user_info['identity_id']==1){
        //体验
        $order_price =$goods_info['experience_member_price'];
    }elseif ($user_info['identity_id']==2){
        //品牌
        $order_price =$goods_info['brand_member_price'];
    }elseif ($user_info['identity_id']==3){
        //店铺
        $order_price =$goods_info['store_price'];
    }elseif ($user_info['identity_id']==4){
        //公司
        $order_price =$goods_info['company_price'];
    }elseif ($user_info['identity_id']==5){
        //合伙人
        $order_price =$goods_info['partner_price'];
    }
    //
    if(!$all_parents or count($all_parents)<=0){
        \Illuminate\Support\Facades\Log::info('复购津贴-没有找到合适的上级反润人');
        return false;
    }
    \Illuminate\Support\Facades\Log::info('去重之前的',[$all_parents]);
    //$all_parents = assoc_unique($all_parents,'id');
    //$all_parents = arrunique($all_parents);

    $temp_arr_parents = [];
        //数组去除重复 (去除身份相同的)
        foreach ($all_parents as $rv){
            if(!array_key_exists($rv['identity_id'],$temp_arr_parents)){
                $temp_arr_parents[$rv['identity_id']] = $rv;
            }
        }
        $all_parents = array_values($temp_arr_parents);

    \Illuminate\Support\Facades\Log::info('去重之后的',[$all_parents]);
    $insert_data = [];
    //这个是数组升序排序 暂时不需要
    //var_dump($all_parents);
//    usort($all_parents, function($a, $b) {
//        return $a['identity_id'] - $b['identity_id'];
//    });

    // 初始化一个空数组来存储过滤后的结果
    $filteredArray = [];

// 初始化一个变量来存储前一个元素的identity_id值
    $previousIdentityId = null;

// 遍历数组
    foreach ($all_parents as $element) {
        // 检查当前元素的identity_id是否大于前一个元素的identity_id
        if ($previousIdentityId === null || $element['identity_id'] > $previousIdentityId) {
            // 如果是，则将其添加到过滤后的数组中
            $filteredArray[] = $element;
            // 更新前一个元素的identity_id值
            $previousIdentityId = $element['identity_id'];
        }
    }
    $all_parents = $filteredArray;


    \Illuminate\Support\Facades\Log::info('复购找出的上级',$all_parents);
    foreach ($all_parents as $ak=>$av){
        if($av['identity_id']==2){


            $price =  $goods_info['brand_member_price'];
            if(isset($all_parents[$ak-1])){
                if($all_parents[$ak-1]['identity_id']==1){
                    //体验
                    $order_price =$goods_info['experience_member_price'];
                }elseif ($all_parents[$ak-1]['identity_id']==2){
                    //品牌
                    $order_price =$goods_info['brand_member_price'];
                }elseif ($all_parents[$ak-1]['identity_id']==3){
                    //店铺
                    $order_price =$goods_info['store_price'];
                }elseif ($all_parents[$ak-1]['identity_id']==4){
                    //公司
                    $order_price =$goods_info['company_price'];
                }elseif ($all_parents[$ak-1]['identity_id']==5){
                    //合伙人
                    $order_price =$goods_info['partner_price'];
                }
                $cha = bcsub($order_price,$goods_info['brand_member_price'],2);
            }else{
                $cha = bcsub($goods_info['experience_member_price'],$goods_info['brand_member_price'],2);
            }
            //差值大于0的
            if($cha>0){
                //$cha = bcsub($order_price,$price,2);
                $tem_money = $cha;
                for ($i=0;$i<$order_info['goods_num'];$i++){
                    $cha = $tem_money;
                    //计算平级的钱
                    $ping_money = bcmul($cha,0.1,2);

                    //echo 123;
                    //平级奖励
                    $ping_res = cultivation_money($av['id'],$ping_money,'复购',$order_info['id']);
                    //没有找到平级的人这个钱不用扣除
                    if($ping_res!==false){
                        $cha = bcsub($cha,$ping_money,2);
                    }

                    //开启了复购券奖励
                    $repeat_money = bcmul($cha,0.05,2);
                    $cha = bcsub($cha,$repeat_money,2);
                    //记录复购券
                    repeatUserMoney($av['id'],$repeat_money,'11',$order_info['id']);

                    //111222
                    $insert_data[] = [
                        'u_id'=>$av['id'],
                        'order_id'=>$order_info['id'],
                        'money'=>$cha,
                        'type'=>6, //复购津贴
                        'symbol'=>1, //收入
                        'status'=>2,//冻结中
                        'remark'=>'复购津贴到账'.$cha.'元',
                        'created_at'=>date('Y-m-d H:i:s',time()),
                        'updated_at'=>date('Y-m-d H:i:s',time())
                    ];


                }
                \Illuminate\Support\Facades\Log::info('复购津贴-上级为品牌会员的人反润',$insert_data);
            }


        }elseif($av['identity_id']==3){
            $price = $goods_info['store_price'];

            if(isset($all_parents[$ak-1])){
                if($all_parents[$ak-1]['identity_id']==1){
                    //体验
                    $order_price =$goods_info['experience_member_price'];
                }elseif ($all_parents[$ak-1]['identity_id']==2){
                    //品牌
                    $order_price =$goods_info['brand_member_price'];
                }elseif ($all_parents[$ak-1]['identity_id']==3){
                    //店铺
                    $order_price =$goods_info['store_price'];
                }elseif ($all_parents[$ak-1]['identity_id']==4){
                    //公司
                    $order_price =$goods_info['company_price'];
                }elseif ($all_parents[$ak-1]['identity_id']==5){
                    //合伙人
                    $order_price =$goods_info['partner_price'];
                }
                $cha = bcsub($order_price,$goods_info['store_price'],2);
            }else{
                //$cha = bcsub($goods_info['experience_member_price'],$goods_info['brand_member_price'],2);
                //$cha = bcsub($goods_info['experience_member_price'],$goods_info['store_price'],2);
                if($user_info['identity_id']==1){
                    //体验
                    $order_price =$goods_info['experience_member_price'];
                }elseif ($user_info['identity_id']==2){
                    //品牌
                    $order_price =$goods_info['brand_member_price'];
                }elseif ($user_info['identity_id']==3){
                    //店铺
                    $order_price =$goods_info['store_price'];
                }elseif ($user_info['identity_id']==4){
                    //公司
                    $order_price =$goods_info['company_price'];
                }elseif ($user_info['identity_id']==5){
                    //合伙人
                    $order_price =$goods_info['partner_price'];
                }
                $cha = bcsub($order_price,$goods_info['store_price'],2);
            }


            //$cha = bcsub($order_price,$price,2);
            if($cha>0){
                $tem_money = $cha;
                for ($i=0;$i<$order_info['goods_num'];$i++){
                    $cha = $tem_money;
                    //计算平级的钱
                    $ping_money = bcmul($cha,0.1,2);

                    //echo 123;
                    //平级奖励
                    $ping_res = cultivation_money($av['id'],$ping_money,'复购',$order_info['id']);
                    //没有找到平级的人这个钱不用扣除
                    if($ping_res!==false){
                        $cha = bcsub($cha,$ping_money,2);
                    }

                    //开启了复购券奖励
                    $repeat_money = bcmul($cha,0.05,2);
                    $cha = bcsub($cha,$repeat_money,2);
                    //记录复购券
                    repeatUserMoney($av['id'],$repeat_money,'11',$order_info['id']);


                    $insert_data[] = [
                        'u_id'=>$av['id'],
                        'order_id'=>$order_info['id'],
                        'money'=>$cha,
                        'type'=>6, //复购津贴
                        'symbol'=>1, //收入
                        'status'=>2,//冻结中
                        'remark'=>'复购津贴到账'.$cha.'元',
                        'created_at'=>date('Y-m-d H:i:s',time()),
                        'updated_at'=>date('Y-m-d H:i:s',time())
                    ];
                }
                \Illuminate\Support\Facades\Log::info('复购津贴-上级为店铺会员的人反润',$insert_data);
            }

        }elseif($av['identity_id']==4){
            $price = $goods_info['company_price'];

            if(isset($all_parents[$ak-1])){
                \Illuminate\Support\Facades\Log::info('wwwwwww'.$all_parents[$ak-1]['identity_id']);
                if($all_parents[$ak-1]['identity_id']==1){
                    //体验
                    $order_price =$goods_info['experience_member_price'];
                }elseif ($all_parents[$ak-1]['identity_id']==2){
                    //品牌
                    $order_price =$goods_info['brand_member_price'];
                }elseif ($all_parents[$ak-1]['identity_id']==3){
                    //店铺
                    $order_price =$goods_info['store_price'];
                }elseif ($all_parents[$ak-1]['identity_id']==4){
                    //公司
                    $order_price =$goods_info['company_price'];
                }elseif ($all_parents[$ak-1]['identity_id']==5){
                    //合伙人
                    $order_price =$goods_info['partner_price'];
                }
                $cha = bcsub($order_price,$goods_info['company_price'],2);
            }else{
                \Illuminate\Support\Facades\Log::info('qqqqqqq');
                //$cha = bcsub($goods_info['experience_member_price'],$goods_info['brand_member_price'],2);
               // $cha = bcsub($goods_info['brand_member_price'],$goods_info['store_price'],2);
                if($goods_info['is_shop_goods']==2){

                    if($user_info['identity_id']==1){
                        //体验
                        $order_price =$goods_info['experience_member_price'];
                    }elseif ($user_info['identity_id']==2){
                        //品牌
                        $order_price =$goods_info['brand_member_price'];
                    }elseif ($user_info['identity_id']==3){
                        //店铺
                        $order_price =$goods_info['store_price'];
                    }elseif ($user_info['identity_id']==4){
                        //公司
                        $order_price =$goods_info['company_price'];
                    }elseif ($user_info['identity_id']==5){
                        //合伙人
                        $order_price =$goods_info['partner_price'];
                    }
                    $cha = bcsub($order_price,$goods_info['company_price'],2);

                    //$cha = bcsub($goods_info['experience_member_price'],$goods_info['company_price'],2);
                }else{
                    //$cha = bcsub($goods_info['experience_member_price'],$goods_info['company_price'],2);
                    if($user_info['identity_id']==1){
                        //体验
                        $order_price =$goods_info['experience_member_price'];
                    }elseif ($user_info['identity_id']==2){
                        //品牌
                        $order_price =$goods_info['brand_member_price'];
                    }elseif ($user_info['identity_id']==3){
                        //店铺
                        $order_price =$goods_info['store_price'];
                    }elseif ($user_info['identity_id']==4){
                        //公司
                        $order_price =$goods_info['company_price'];
                    }elseif ($user_info['identity_id']==5){
                        //合伙人
                        $order_price =$goods_info['partner_price'];
                    }
                    $cha = bcsub($order_price,$goods_info['company_price'],2);
                }

            }
            \Illuminate\Support\Facades\Log::info('复购津贴-上级为公司会员的人反润金额'.$cha);
            if($cha>0){
                $tem_money = $cha;
                //$cha = bcsub($order_price,$price,2);
                for ($i=0;$i<$order_info['goods_num'];$i++){
                    $cha = $tem_money;
                    //计算平级的钱
                    $ping_money = bcmul($cha,0.1,2);

                    //echo 123;
                    //平级奖励
                    $ping_res = cultivation_money($av['id'],$ping_money,'复购',$order_info['id']);
                    //没有找到平级的人这个钱不用扣除
                    if($ping_res!==false){
                        $cha = bcsub($cha,$ping_money,2);
                    }

                    //开启了复购券奖励
                    $repeat_money = bcmul($cha,0.05,2);
                    $cha = bcsub($cha,$repeat_money,2);
                    //记录复购券
                    repeatUserMoney($av['id'],$repeat_money,'',$order_info['id']);

                    $insert_data[] = [
                        'u_id'=>$av['id'],
                        'order_id'=>$order_info['id'],
                        'money'=>$cha,
                        'type'=>6, //复购津贴
                        'symbol'=>1, //收入
                        'status'=>2,//冻结中
                        'remark'=>'复购津贴到账'.$cha.'元',
                        'created_at'=>date('Y-m-d H:i:s',time()),
                        'updated_at'=>date('Y-m-d H:i:s',time())
                    ];
                }
                \Illuminate\Support\Facades\Log::info('复购津贴-上级为公司会员的人反润',$insert_data);
            }

        }elseif($av['identity_id']==5){
            $price = $goods_info['partner_price'];

            if(isset($all_parents[$ak-1])){
                if($all_parents[$ak-1]['identity_id']==1){
                    //体验
                    $order_price =$goods_info['experience_member_price'];
                }elseif ($all_parents[$ak-1]['identity_id']==2){
                    //品牌
                    $order_price =$goods_info['brand_member_price'];
                }elseif ($all_parents[$ak-1]['identity_id']==3){
                    //店铺
                    $order_price =$goods_info['store_price'];
                }elseif ($all_parents[$ak-1]['identity_id']==4){
                    //公司
                    $order_price =$goods_info['company_price'];
                }elseif ($all_parents[$ak-1]['identity_id']==5){
                    //合伙人
                    $order_price =$goods_info['partner_price'];
                }
                $cha = bcsub($order_price,$goods_info['partner_price'],2);
            }else{
                //$cha = bcsub($goods_info['experience_member_price'],$goods_info['brand_member_price'],2);
                // $cha = bcsub($goods_info['brand_member_price'],$goods_info['store_price'],2);
               // $cha = bcsub($goods_info['store_price'],$goods_info['company_price'],2);
                $cha = bcsub($goods_info['experience_member_price'],$goods_info['partner_price'],2);

                if($goods_info['is_shop_goods']==2){
                    if($user_info['identity_id']==1){
                        //体验
                        $order_price =$goods_info['experience_member_price'];
                    }elseif ($user_info['identity_id']==2){
                        //品牌
                        $order_price =$goods_info['brand_member_price'];
                    }elseif ($user_info['identity_id']==3){
                        //店铺
                        $order_price =$goods_info['store_price'];
                    }elseif ($user_info['identity_id']==4){
                        //公司
                        $order_price =$goods_info['company_price'];
                    }elseif ($user_info['identity_id']==5){
                        //合伙人
                        $order_price =$goods_info['partner_price'];
                    }
                    $cha = bcsub($order_price,$goods_info['partner_price'],2);
                    //$cha = bcsub($goods_info['experience_member_price'],$goods_info['partner_price'],2);
                }else{
                    //$cha = bcsub($goods_info['experience_member_price'],$goods_info['company_price'],2);
                    //$cha = bcsub($goods_info['experience_member_price'],$goods_info['partner_price'],2);
                    if($user_info['identity_id']==1){
                        //体验
                        $order_price =$goods_info['experience_member_price'];
                    }elseif ($user_info['identity_id']==2){
                        //品牌
                        $order_price =$goods_info['brand_member_price'];
                    }elseif ($user_info['identity_id']==3){
                        //店铺
                        $order_price =$goods_info['store_price'];
                    }elseif ($user_info['identity_id']==4){
                        //公司
                        $order_price =$goods_info['company_price'];
                    }elseif ($user_info['identity_id']==5){
                        //合伙人
                        $order_price =$goods_info['partner_price'];
                    }
                    $cha = bcsub($order_price,$goods_info['partner_price'],2);
                }
            }

            if($cha>0){
                $tem_money = $cha;
                // $cha = bcsub($order_price,$price,2);
                for ($i=0;$i<$order_info['goods_num'];$i++){
                    $cha = $tem_money;
                    //计算平级的钱
                    $ping_money = bcmul($cha,0.1,2);

                    //echo 123;
                    //平级奖励
                    $ping_res = cultivation_money($av['id'],$ping_money,'复购',$order_info['id']);
                    //没有找到平级的人这个钱不用扣除
                    if($ping_res!==false){
                        $cha = bcsub($cha,$ping_money,2);
                    }

                    //开启了复购券奖励
                    $repeat_money = bcmul($cha,0.05,2);
                    $cha = bcsub($cha,$repeat_money,2);
                    //记录复购券
                    repeatUserMoney($av['id'],$repeat_money,'11',$order_info['id']);

                    $insert_data[] = [
                        'u_id'=>$av['id'],
                        'order_id'=>$order_info['id'],
                        'money'=>$cha,
                        'type'=>6, //复购津贴
                        'symbol'=>1, //收入
                        'status'=>2,//冻结中
                        'remark'=>'复购津贴到账'.$cha.'元',
                        'created_at'=>date('Y-m-d H:i:s',time()),
                        'updated_at'=>date('Y-m-d H:i:s',time())
                    ];
                }
                \Illuminate\Support\Facades\Log::info('复购津贴-上级为合伙人会员的人反润',$insert_data);
            }

        }
    }

    //var_dump($insert_data);
    \App\Models\TongUserProfit::query()->insert($insert_data);
}


/**
 * 直推奖励的差集
 * 向上找出比自己身份高的用户
 * 如果
 * @return void
 */
function recommend_money_child_cha($user_id,string $ind_key='1-1',$order_id=0,$up_money){

    \Illuminate\Support\Facades\Log::info('直推差集执行qqqqqqqq');
    \Illuminate\Support\Facades\Log::info('邀请关系'.$ind_key);
    //获取用户信息
    //$user_info =
    $user_list = getRedisUserData();
    \Illuminate\Support\Facades\Log::info('获取到的redis用户信息',[$user_list]);
    \Illuminate\Support\Facades\Log::info('获取用户'.$user_id.'的上级');
    $all_parents = getAllParents($user_list, $user_id);



    //$all_parents = array_unique($all_parents);
    //找出用户身份比自己等级高的
    if(empty($all_parents)){
        \Illuminate\Support\Facades\Log::info('直推奖励差集没有找到父级');
        return false;
    }

    $temp_arr_parents = [];
    //数组去除重复
    foreach ($all_parents as $rv){
        if(!array_key_exists($rv['id'],$temp_arr_parents)){
            $temp_arr_parents[$rv['id']] = $rv;
        }
    }
    $all_parents = array_values($temp_arr_parents);

    //先获取出自己的身份
    $self_identity_id = $all_parents[0]['identity_id'];
    if($self_identity_id==5){
        \Illuminate\Support\Facades\Log::info('当前用户是最高身份不用补差');
        return false;
    }
    \Illuminate\Support\Facades\Log::info('直推人自己的身份'.$self_identity_id);

    \Illuminate\Support\Facades\Log::info('找到了未处理的上级用户',$all_parents);

    foreach ($all_parents as $tv){
        \Illuminate\Support\Facades\Log::info('找到了未处理的上级用户'.$tv['id']);
    }
    //var_dump($all_parents);
    //删除掉自己
    $self_arr =$all_parents[0];
    unset($all_parents[0]);
    $start_index = -1;
    //重置数组下标
    //var_dump($self_identity_id);
    $all_parents = array_values($all_parents);
    //这里是只找身份比自己高的
//    foreach ($all_parents as $key=>$val){
//        //echo $val['identity_id'].$self_identity_id;
//        if($val['identity_id']>$self_identity_id){
//            $start_index = $key;
//            break;
//        }
//    }
    //这里先换成fil
    $all_parents = array_filter($all_parents,function($ele) use($self_identity_id){
        if($ele['identity_id']>$self_identity_id){
            return $ele;
        }
    });
    $all_parents = array_values($all_parents);

    \Illuminate\Support\Facades\Log::info('第一次处理上级用户 start_index',$all_parents);
    //数组截取
    if($start_index!=-1){
        $all_parents = array_slice($all_parents, $start_index, count($all_parents));
        $all_parents = array_values($all_parents);
    }else{
        $all_parents = array_slice($all_parents, 0, count($all_parents));
        $all_parents = array_values($all_parents);
    }
    \Illuminate\Support\Facades\Log::info('找到了处理后的上级用户',$all_parents);

    //过来掉里面不是连续的身份标识
    $all_parents = array_filter($all_parents,function($item){
        return in_array($item['identity_id'],[2,3,4,5]);
    });
    $all_parents = array_values($all_parents);
    //$jieguo_res = [];
    if(config('user.recommend.jicha_lianxu')){

        //需要找出连续的
//        foreach ($all_parents as $jk=>$jv){
//            if()
//            if($jv['p_id']!=$all_parents[$jk]['id']){
//                break;
//            }
//        }
        $a_count = count($all_parents);
        //echo 'a'.$a_count;
        //var_dump($all_parents);
        $end_index = 0;
        for ($jk=0;$jk<=$a_count;$jk++){
            //dd($all_parents[$jk],$all_parents[$jk+1]);
            if(isset($all_parents[$jk+1])){
                if($all_parents[$jk]['p_id']!=$all_parents[$jk+1]['id']){
                    break;
                }
            }
            $end_index = $jk;
        }

        //dd($end_index,$all_parents);

        $all_parents = array_slice($all_parents, 0, $end_index);
        $all_parents = array_values($all_parents);


    }else{
        $all_parents = array_unique_by_key($all_parents,'identity_id');
        //暂时先注释掉
//        $temp_arr = []; //里面存放用户身份
//        //只找出身份最近的人身份比自己高的（最高找到合伙人）
//        foreach ($all_parents as $k=>$v){
//            if(!in_array($v['identity_id'],$temp_arr)){
//                //array_push($temp_arr,$v['identity_id']);
//                $temp_arr[] = $v['identity_id'];
//            }else{
//                unset($all_parents[$key]);
//            }
//        }
//        $all_parents = array_values($all_parents);
    }

    \Illuminate\Support\Facades\Log::info('找到的最近身份的人推荐补差',[$all_parents]);
    //去除掉身份相同的
//    $temp_arr_parents = [];
//    //数组去除重复
//    foreach ($all_parents as $rv){
//        if(!array_key_exists($rv['identity_id'],$temp_arr_parents)){
//            $temp_arr_parents[$rv['identity_id']] = $rv;
//        }
//    }
    $all_parents = array_unique_by_key($all_parents,'identity_id');
    $all_parents = array_values($all_parents);
    \Illuminate\Support\Facades\Log::info('找到的最近身份的人推荐补差身份去除重复的',[$all_parents]);

    //查看这个用户推荐了一个什么身份的人
    //if(explode())
    $now_key = explode('-',$ind_key)[1];

    \Illuminate\Support\Facades\Log::info('找到的直推人上级的人列表',$all_parents);

    foreach ($all_parents as $tk=>$tv){
        \Illuminate\Support\Facades\Log::info('找到的直推人上级的人列表temp'.$tk.'===='.$tv['identity_id']);
    }

    $all_parents = removeSmallerNeighbors($all_parents,'identity_id');

    //array_unshift($all_parents,$self_arr);
    $all_parents = array_values($all_parents);
    //\Illuminate\Support\Facades\Log::info('找差别插入自己以后',);
    \Illuminate\Support\Facades\Log::info('找到的直推人上级的人列表222',$all_parents);
    $inset_data = [];
    foreach ($all_parents as $lk=>$lv){
        $for_now_key = $lv['identity_id'].'-'.$now_key;
        //var_dump($for_now_key);
        if($lk==0){
            $up_now_key = $ind_key;
           // continue;
        }else{
            $up_now_key = $all_parents[$lk-1]['identity_id'].'-'.$now_key;
        }

        $for_now_money = config('user.recommend')[$for_now_key];
        $up_now_money = config('user.recommend')[$up_now_key];
        $money = bcsub($for_now_money,$up_now_money,2);

        \Illuminate\Support\Facades\Log::info('hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh找除了直推上级的用户给他们补钱'.$lv['id'],[
            config('user.recommend')[$for_now_key],
            config('user.recommend')[$up_now_key],
            $money
        ]);
        //123
        \Illuminate\Support\Facades\Log::info('推荐补差用户'.$lv['id'].'金额'.$money);

        $order_info = TongOrder::query()->where('id',$order_id)->first();
        \Illuminate\Support\Facades\Log::info('我执行到了这里啦啦啦啦啦啦99999999999999999999999999999999999999999999999999999999999999999999999999999999');
        if(in_array((int)$order_info['order_price'],[3581,9401,5820])){
            \Illuminate\Support\Facades\Log::info('用户补差升级执行');
            //体验升级=3980
            if($order_info['order_price']==3581){
                \Illuminate\Support\Facades\Log::info('3581111111111111111111111111111111111111111111111111111111111');
                if(isset($all_parents[$lk-1])){
                    \Illuminate\Support\Facades\Log::info('3581isset');
                    if ($all_parents[$lk - 1]['identity_id'] == 1 && $all_parents[$lk]['identity_id'] == 2) {
                        $money = 1070;
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 1 && $all_parents[$lk]['identity_id'] == 3) {
                        $money = 1250;
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 1 && $all_parents[$lk]['identity_id'] == 4) {
                        $money = 1430;
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 1 && $all_parents[$lk]['identity_id'] == 5) {
                        $money = 1610;
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 2 && $all_parents[$lk]['identity_id'] == 3) {
                        $money = 180;
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 2 && $all_parents[$lk]['identity_id'] == 4) {
                        $money = 360;
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 2 && $all_parents[$lk]['identity_id'] == 5) {
                        $money = 540;
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 3 && $all_parents[$lk]['identity_id'] == 4) {
                        $money = 180;
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 3 && $all_parents[$lk]['identity_id'] == 5) {
                        $money = 360;
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 4 && $all_parents[$lk]['identity_id'] == 5) {
                        $money = 180;
                    }
                }else{
                    \Illuminate\Support\Facades\Log::info('3581else-'.$all_parents[$lk]['identity_id']);
                    if($all_parents[$lk]['identity_id']==2){

                        $money = 1070-$up_money;
                        \Illuminate\Support\Facades\Log::info('3581sele1070'.$money);
                    }
                    if($all_parents[$lk]['identity_id']==3){
                        $money = 1250-$up_money;
                        \Illuminate\Support\Facades\Log::info('3581sele1250'.$money);
                    }
                    if($all_parents[$lk]['identity_id']==4){
                        $money = 1430-$up_money;
                        \Illuminate\Support\Facades\Log::info('3581sele1430'.$money);
                    }
                    if($all_parents[$lk]['identity_id']==5){
                        $money = 1610-$up_money;
                        \Illuminate\Support\Facades\Log::info('3581sele1610'.$money);
                    }
                }
                \Illuminate\Support\Facades\Log::info('用户补差升级执行-3581-'.$money);
            }
            //399-直接升级9800
            if($order_info['order_price']==9401){
                \Illuminate\Support\Facades\Log::info('9401111111111111111111111111111111111111111111111111111111111');
                if(isset($all_parents[$lk-1])) {


                    if ($all_parents[$lk - 1]['identity_id'] == 1 && $all_parents[$lk]['identity_id'] == 2) {
                        $money = 2820;
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 1 && $all_parents[$lk]['identity_id'] == 3) {
                        $money = 3290;
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 1 && $all_parents[$lk]['identity_id'] == 4) {
                        $money = 3760;
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 1 && $all_parents[$lk]['identity_id'] == 5) {
                        $money = 4230;
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 2 && $all_parents[$lk]['identity_id'] == 3) {
                        $money = 470;
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 2 && $all_parents[$lk]['identity_id'] == 4) {
                        $money = 940;
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 2 && $all_parents[$lk]['identity_id'] == 5) {
                        $money = 1410;
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 3 && $all_parents[$lk]['identity_id'] == 4) {
                        $money = 470;
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 3 && $all_parents[$lk]['identity_id'] == 5) {
                        $money = 940;
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 4 && $all_parents[$lk]['identity_id'] == 5) {
                        $money = 470;
                    }
                }else{
                    if($all_parents[$lk]['identity_id']==2){
                        $money = 2820-$up_money;
                    }
                    if($all_parents[$lk]['identity_id']==3){
                        $money = 3290-$up_money;
                    }
                    if($all_parents[$lk]['identity_id']==4){
                        $money = 3760-$up_money;
                    }
                    if($all_parents[$lk]['identity_id']==5){
                        $money = 4230-$up_money;
                    }
                }
                \Illuminate\Support\Facades\Log::info('用户补差升级执行-9401-'.$money);
            }

            //品牌升级9800
            if($order_info['order_price']==5820){
                \Illuminate\Support\Facades\Log::info('5820111111111111111111111111111111111111111111111111111111111');
                if(isset($all_parents[$lk-1])) {
                    if ($all_parents[$lk - 1]['identity_id'] == 1 && $all_parents[$lk]['identity_id'] == 2) {
                        $money = 1750;
                        //$money = 1750-400;
                        \Illuminate\Support\Facades\Log::info('5820执行aa'.$lv['id'].$money);
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 1 && $all_parents[$lk]['identity_id'] == 3) {
                        $money = 2040;
                        //$money = 2040-400;
                        \Illuminate\Support\Facades\Log::info('5820执行bb'.$lv['id'].$money);
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 1 && $all_parents[$lk]['identity_id'] == 4) {
                        $money = 2330;
                        //$money = 2330-400;
                        \Illuminate\Support\Facades\Log::info('5820执行cc'.$lv['id'].$money);
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 1 && $all_parents[$lk]['identity_id'] == 5) {
                        $money = 2620;
                        //$money = 2620-400;
                        \Illuminate\Support\Facades\Log::info('5820执行dd'.$lv['id'].$money);
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 2 && $all_parents[$lk]['identity_id'] == 3) {
                        $money = 290;
                        //$money = 999;
                        \Illuminate\Support\Facades\Log::info('5820执行ee'.$lv['id'].$money);
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 2 && $all_parents[$lk]['identity_id'] == 4) {
                        $money = 180;
                        \Illuminate\Support\Facades\Log::info('5820执行ff'.$lv['id'].$money);
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 2 && $all_parents[$lk]['identity_id'] == 5) {
                        $money = 470;
                        \Illuminate\Support\Facades\Log::info('5820执行gg'.$lv['id'].$money);
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 3 && $all_parents[$lk]['identity_id'] == 4) {
                        $money = 290;
                        \Illuminate\Support\Facades\Log::info('5820执行hh'.$lv['id'].$money);
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 3 && $all_parents[$lk]['identity_id'] == 5) {
                        $money = 580;
                        \Illuminate\Support\Facades\Log::info('5820执行ii'.$lv['id'].$money);
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 4 && $all_parents[$lk]['identity_id'] == 5) {
                        $money = 290;
                        \Illuminate\Support\Facades\Log::info('5820执行kk'.$lv['id'].$money);
                    }
                }else{
                    if($all_parents[$lk]['identity_id']==2){
                       // $money = 1750-$up_money-400;
                        //暂时先把-400去掉
                        $money = 1750-$up_money;
                        \Illuminate\Support\Facades\Log::info('5820执行qq'.$lv['id'].$money);
                    }
                    if($all_parents[$lk]['identity_id']==3){
                        //$money = 2040;
                        //$money = 2040-$up_money-400;
                        //暂时先把-400去掉
                        $money = 2040-$up_money;
                        \Illuminate\Support\Facades\Log::info('5820执行ww'.$lv['id'].$money);
                    }
                    if($all_parents[$lk]['identity_id']==4){
                        //$money = 2330-$up_money-400;
                        //$money = 2330-$up_money-400;
                        //暂时先把-400去掉
                        $money = 2330-$up_money;
                        \Illuminate\Support\Facades\Log::info('5820执行tt'.$lv['id'].$money);
                    }
                    if($all_parents[$lk]['identity_id']==5){
                        //$money = 2620-$up_money-400;
                        //暂时先把-400去掉
                        $money = 2620-$up_money;
                        //$money = 2620-$up_money-400;
                        \Illuminate\Support\Facades\Log::info('5820执行yy'.$lv['id'].$money);
                    }
                }
                \Illuminate\Support\Facades\Log::info('用户补差升级执行-5820-'.$money);
            }


        }
        \Illuminate\Support\Facades\Log::info('if判断上的money'.$lv['id'].'=='.$money);
        if($money>0){

            //开启了平级奖励
            if(config('user.recommend.jicha_ping')){
                //计算平级的钱
                $ping_money = bcmul($money,0.1,2);

                //平级奖励
                $ping_res = cultivation_money($lv['id'],$ping_money,'推荐销售补差',$order_info['id']);
                //没有找到平级的人这个钱不用扣除
                if($ping_res!==false){
                    $money = bcsub($money,$ping_money,2);
                }
            }

            //这个钱拿出百分之5作为复购券
            if(config('user.recommend.jicha_fugou')){
                //开启了复购券奖励
                $repeat_money = bcmul($money,0.05,2);
                $money = bcsub($money,$repeat_money,2);
                //记录复购券
                repeatUserMoney($lv['id'],$repeat_money,'推荐销售补差',$order_info['id']);
            }

            //如果上面存在认证的店铺就把体验店的人id 替换成为认证店铺的人
//            $auth_store_id = findAuthStore($user_id);
//            if($auth_store_id){
//                //如果找到了认证店铺 并且 $up_user_info['identity_id']==3
//                if($lv['identity_id']==3){
//                    //那就把钱给到这个认证店铺 其他正常给他们返钱
//                    $lv['id'] = $auth_store_id;
//                }
//
//            }

            $inset_data[] = [
                'u_id'=>$lv['id'],
                'money'=>$money,
                'type'=>13, //推荐补差
                'order_id'=>$order_info['id'],
                'symbol'=>1,//收入
                'status'=>2,//显示冻结中
                'remark'=>'推荐销售津贴补差价'.$money,
                'created_at'=>date('Y-m-d H:i:s'),
                'updated_at'=>date('Y-m-d H:i:s')
            ];
            \Illuminate\Support\Facades\Log::info('最终插入的数据',$inset_data);

        }

    }
    //插入数据
    \App\Models\TongUserProfit::query()->insert($inset_data);


}



function recommend_money_child_cha_tow($user_id,string $ind_key='1-1',$order_id=0,$up_money){

    \Illuminate\Support\Facades\Log::info('我执行的是recommend_money_child_cha_tow这个方法');

    //获取用户信息
    $user_list = getRedisUserData();
    $all_parents = getAllParents($user_list, $user_id);

    //找出用户身份比自己等级高的
    if(empty($all_parents)){
        \Illuminate\Support\Facades\Log::info('直推奖励差集没有找到父级');
        return false;
    }

    $temp_arr_parents = [];
    //数组去除重复
    foreach ($all_parents as $rv){
        if(!array_key_exists($rv['id'],$temp_arr_parents)){
            $temp_arr_parents[$rv['id']] = $rv;
        }
    }
    $all_parents = array_values($temp_arr_parents);

    //先获取出自己的身份
    $self_identity_id = $all_parents[0]['identity_id'];
    if($self_identity_id==5){
        \Illuminate\Support\Facades\Log::info('当前用户是最高身份不用补差');
        return false;
    }

    //删除掉自己
    $self_arr =$all_parents[0];
    unset($all_parents[0]);
    $start_index = -1;
    //重置数组下标
    //var_dump($self_identity_id);
    $all_parents = array_values($all_parents);
    //这里是只找身份比自己高的
    //这里先换成fil
    $all_parents = array_filter($all_parents,function($ele) use($self_identity_id){
        if($ele['identity_id']>$self_identity_id){
            return $ele;
        }
    });
    $all_parents = array_values($all_parents);

    //数组截取
    if($start_index!=-1){
        $all_parents = array_slice($all_parents, $start_index, count($all_parents));
        $all_parents = array_values($all_parents);
    }else{
        $all_parents = array_slice($all_parents, 0, count($all_parents));
        $all_parents = array_values($all_parents);
    }

    //过来掉里面不是连续的身份标识
    $all_parents = array_filter($all_parents,function($item){
        return in_array($item['identity_id'],[2,3,4,5]);
    });
    $all_parents = array_values($all_parents);
    //$jieguo_res = [];
    if(config('user.recommend.jicha_lianxu')){

        $a_count = count($all_parents);
        //echo 'a'.$a_count;
        //var_dump($all_parents);
        $end_index = 0;
        for ($jk=0;$jk<=$a_count;$jk++){
            //dd($all_parents[$jk],$all_parents[$jk+1]);
            if(isset($all_parents[$jk+1])){
                if($all_parents[$jk]['p_id']!=$all_parents[$jk+1]['id']){
                    break;
                }
            }
            $end_index = $jk;
        }

        //dd($end_index,$all_parents);

        $all_parents = array_slice($all_parents, 0, $end_index);
        $all_parents = array_values($all_parents);


    }else{
        $all_parents = array_unique_by_key($all_parents,'identity_id');
    }

    $all_parents = array_unique_by_key($all_parents,'identity_id');
    $all_parents = array_values($all_parents);
    \Illuminate\Support\Facades\Log::info('找到的最近身份的人推荐补差身份去除重复的',[$all_parents]);

    //查看这个用户推荐了一个什么身份的人
    //if(explode())
    $now_key = explode('-',$ind_key)[1];

    \Illuminate\Support\Facades\Log::info('找到的直推人上级的人列表',$all_parents);


    $all_parents = removeSmallerNeighbors($all_parents,'identity_id');

    $all_parents = array_values($all_parents);
    \Illuminate\Support\Facades\Log::info('找到的直推人上级的人列表222',$all_parents);
    $inset_data = [];
    foreach ($all_parents as $lk=>$lv){
        $for_now_key = $lv['identity_id'].'-'.$now_key;
        //var_dump($for_now_key);
        if($lk==0){
            $up_now_key = $ind_key;
            // continue;
        }else{
            $up_now_key = $all_parents[$lk-1]['identity_id'].'-'.$now_key;
        }

        $for_now_money = config('user.recommend')[$for_now_key];
        $up_now_money = config('user.recommend')[$up_now_key];
        $money = bcsub($for_now_money,$up_now_money,2);

        $money = 0;

        \Illuminate\Support\Facades\Log::info('hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh找除了直推上级的用户给他们补钱'.$lv['id'],[
            config('user.recommend')[$for_now_key],
            config('user.recommend')[$up_now_key],
            $money
        ]);
        //123
        \Illuminate\Support\Facades\Log::info('推荐补差用户'.$lv['id'].'金额'.$money);

        $order_info = TongOrder::query()->where('id',$order_id)->first();
        if(in_array((int)$order_info['order_price'],[3581,9401,5820])){
            \Illuminate\Support\Facades\Log::info('用户补差升级执行');
            //体验升级=3980
            if($order_info['order_price']==3581){
                \Illuminate\Support\Facades\Log::info('3581111111111111111111111111111111111111111111111111111111111');
                if(isset($all_parents[$lk-1])){
                    \Illuminate\Support\Facades\Log::info('3581isset');
                    if ($all_parents[$lk - 1]['identity_id'] == 1 && $all_parents[$lk]['identity_id'] == 2) {
                        $money = 1070;
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 1 && $all_parents[$lk]['identity_id'] == 3) {
                        $money = 1250;
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 1 && $all_parents[$lk]['identity_id'] == 4) {
                        $money = 1430;
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 1 && $all_parents[$lk]['identity_id'] == 5) {
                        $money = 1610;
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 2 && $all_parents[$lk]['identity_id'] == 3) {
                        $money = 180;
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 2 && $all_parents[$lk]['identity_id'] == 4) {
                        $money = 360;
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 2 && $all_parents[$lk]['identity_id'] == 5) {
                        $money = 540;
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 3 && $all_parents[$lk]['identity_id'] == 4) {
                        $money = 180;
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 3 && $all_parents[$lk]['identity_id'] == 5) {
                        $money = 360;
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 4 && $all_parents[$lk]['identity_id'] == 5) {
                        $money = 180;
                    }
                }else{
                    \Illuminate\Support\Facades\Log::info('3581else-'.$all_parents[$lk]['identity_id']);
                    if($all_parents[$lk]['identity_id']==2){

                        $money = 1070-$up_money;
                        \Illuminate\Support\Facades\Log::info('3581sele1070'.$money);
                    }
                    if($all_parents[$lk]['identity_id']==3){
                        $money = 1250-$up_money;
                        \Illuminate\Support\Facades\Log::info('3581sele1250'.$money);
                    }
                    if($all_parents[$lk]['identity_id']==4){
                        $money = 1430-$up_money;
                        \Illuminate\Support\Facades\Log::info('3581sele1430'.$money);
                    }
                    if($all_parents[$lk]['identity_id']==5){
                        $money = 1610-$up_money;
                        \Illuminate\Support\Facades\Log::info('3581sele1610'.$money);
                    }
                }
                \Illuminate\Support\Facades\Log::info('用户补差升级执行-3581-'.$money);
            }
            //399-直接升级9800
            if($order_info['order_price']==9401){
                \Illuminate\Support\Facades\Log::info('9401111111111111111111111111111111111111111111111111111111111');
                if(isset($all_parents[$lk-1])) {


                    if ($all_parents[$lk - 1]['identity_id'] == 1 && $all_parents[$lk]['identity_id'] == 2) {
                        $money = 2820;
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 1 && $all_parents[$lk]['identity_id'] == 3) {
                        $money = 3290;
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 1 && $all_parents[$lk]['identity_id'] == 4) {
                        $money = 3760;
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 1 && $all_parents[$lk]['identity_id'] == 5) {
                        $money = 4230;
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 2 && $all_parents[$lk]['identity_id'] == 3) {
                        $money = 470;
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 2 && $all_parents[$lk]['identity_id'] == 4) {
                        $money = 940;
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 2 && $all_parents[$lk]['identity_id'] == 5) {
                        $money = 1410;
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 3 && $all_parents[$lk]['identity_id'] == 4) {
                        $money = 470;
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 3 && $all_parents[$lk]['identity_id'] == 5) {
                        $money = 940;
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 4 && $all_parents[$lk]['identity_id'] == 5) {
                        $money = 470;
                    }
                }else{
                    if($all_parents[$lk]['identity_id']==2){
                        $money = 2820-$up_money;
                    }
                    if($all_parents[$lk]['identity_id']==3){
                        $money = 3290-$up_money;
                    }
                    if($all_parents[$lk]['identity_id']==4){
                        $money = 3760-$up_money;
                    }
                    if($all_parents[$lk]['identity_id']==5){
                        $money = 4230-$up_money;
                    }
                }
                \Illuminate\Support\Facades\Log::info('用户补差升级执行-9401-'.$money);
            }

            //品牌升级9800
            if($order_info['order_price']==5820){
                \Illuminate\Support\Facades\Log::info('5820111111111111111111111111111111111111111111111111111111111');
                if(isset($all_parents[$lk-1])) {
                    if ($all_parents[$lk - 1]['identity_id'] == 1 && $all_parents[$lk]['identity_id'] == 2) {
                        $money = 1750;
                        //$money = 1750-400;
                        \Illuminate\Support\Facades\Log::info('5820执行aa'.$lv['id'].$money);
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 1 && $all_parents[$lk]['identity_id'] == 3) {
                        $money = 2040;
                        //$money = 2040-400;
                        \Illuminate\Support\Facades\Log::info('5820执行bb'.$lv['id'].$money);
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 1 && $all_parents[$lk]['identity_id'] == 4) {
                        $money = 2330;
                        //$money = 2330-400;
                        \Illuminate\Support\Facades\Log::info('5820执行cc'.$lv['id'].$money);
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 1 && $all_parents[$lk]['identity_id'] == 5) {
                        $money = 2620;
                        //$money = 2620-400;
                        \Illuminate\Support\Facades\Log::info('5820执行dd'.$lv['id'].$money);
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 2 && $all_parents[$lk]['identity_id'] == 3) {
                        $money = 290;
                        //$money = 999;
                        \Illuminate\Support\Facades\Log::info('5820执行ee'.$lv['id'].$money);
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 2 && $all_parents[$lk]['identity_id'] == 4) {
                        $money = 180;
                        \Illuminate\Support\Facades\Log::info('5820执行ff'.$lv['id'].$money);
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 2 && $all_parents[$lk]['identity_id'] == 5) {
                        $money = 470;
                        \Illuminate\Support\Facades\Log::info('5820执行gg'.$lv['id'].$money);
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 3 && $all_parents[$lk]['identity_id'] == 4) {
                        $money = 290;
                        \Illuminate\Support\Facades\Log::info('5820执行hh'.$lv['id'].$money);
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 3 && $all_parents[$lk]['identity_id'] == 5) {
                        $money = 580;
                        \Illuminate\Support\Facades\Log::info('5820执行ii'.$lv['id'].$money);
                    } elseif ($all_parents[$lk - 1]['identity_id'] == 4 && $all_parents[$lk]['identity_id'] == 5) {
                        $money = 290;
                        \Illuminate\Support\Facades\Log::info('5820执行kk'.$lv['id'].$money);
                    }
                }else{
                    if($all_parents[$lk]['identity_id']==2){
                        // $money = 1750-$up_money-400;
                        //暂时先把-400去掉
                        $money = 1750-$up_money;
                        \Illuminate\Support\Facades\Log::info('5820执行qq'.$lv['id'].$money);
                    }
                    if($all_parents[$lk]['identity_id']==3){
                        //$money = 2040;
                        //$money = 2040-$up_money-400;
                        //暂时先把-400去掉
                        $money = 2040-$up_money;
                        \Illuminate\Support\Facades\Log::info('5820执行ww'.$lv['id'].$money);
                    }
                    if($all_parents[$lk]['identity_id']==4){
                        //$money = 2330-$up_money-400;
                        //$money = 2330-$up_money-400;
                        //暂时先把-400去掉
                        $money = 2330-$up_money;
                        \Illuminate\Support\Facades\Log::info('5820执行tt'.$lv['id'].$money);
                    }
                    if($all_parents[$lk]['identity_id']==5){
                        //$money = 2620-$up_money-400;
                        //暂时先把-400去掉
                        $money = 2620-$up_money;
                        //$money = 2620-$up_money-400;
                        \Illuminate\Support\Facades\Log::info('5820执行yy'.$lv['id'].$money);
                    }
                }
                \Illuminate\Support\Facades\Log::info('用户补差升级执行-5820-'.$money);
            }


        }
        \Illuminate\Support\Facades\Log::info('if判断上的money'.$lv['id'].'=='.$money);
        if($money>0){

            //这个钱拿出百分之5作为复购券
            if(config('user.recommend.jicha_fugou')){
                //开启了复购券奖励
                $repeat_money = bcmul($money,0.05,2);
                $money = bcsub($money,$repeat_money,2);
                //记录复购券
                repeatUserMoney($lv['id'],$repeat_money,'推荐销售补差');
            }
            //开启了平级奖励
            if(config('user.recommend.jicha_ping')){
                //计算平级的钱
                $ping_money = bcmul($money,0.1,2);

                //平级奖励
                $ping_res = cultivation_money($lv['id'],$ping_money,'推荐销售补差');
                //没有找到平级的人这个钱不用扣除
                if($ping_res!==false){
                    $money = bcsub($money,$ping_money,2);
                }
            }
            $inset_data[] = [
                'u_id'=>$lv['id'],
                'money'=>$money,
                'type'=>13, //推荐补差
                'symbol'=>1,//收入
                'status'=>2,//显示冻结中
                'remark'=>'推荐销售津贴补差价'.$money,
                'created_at'=>date('Y-m-d H:i:s'),
                'updated_at'=>date('Y-m-d H:i:s')
            ];
            \Illuminate\Support\Facades\Log::info('最终插入的数据',$inset_data);

        }

    }
    //插入数据
    \App\Models\TongUserProfit::query()->insert($inset_data);


}

/**
 * 用户直推奖励）
 * @return false
 */
function recommend_money($user_id,$order_id){

    //获取用户详情
    $user_info = TongUser::query()->where('id',$user_id)->first();
    if(config('user.recommend.is_next_user')==false){
        if($user_info['identity_id']==0){
            return false;
        }
    }

    //获取上级用户
    $up_user_info = TongUser::query()->where('id',$user_info['p_id'])->first();
    if(config('user.recommend.is_next_user')==true) {


        if ($up_user_info['identity_id'] == 0) {
            //继续向上找比他身份打的人

            $user_list = getRedisUserData();
            $all_parents = getAllParents($user_list, $user_id);
            \Illuminate\Support\Facades\Log::info('隔级查找1111');
            \Illuminate\Support\Facades\Log::info('查找的用户id',[$user_id]);
            \Illuminate\Support\Facades\Log::info('找到的所有父级',[$all_parents]);
            $temp_arr_parents = [];
            //数组去除重复
            foreach ($all_parents as $rv) {
                if (!array_key_exists($rv['id'], $temp_arr_parents)) {
                    $temp_arr_parents[$rv['id']] = $rv;
                }
            }
            $all_parents = array_values($temp_arr_parents);

            \Illuminate\Support\Facades\Log::info('找到的所有父级去除重复后的',[$all_parents]);

            //先获取出自己的身份
            $self_identity_id = $all_parents[0]['identity_id'];
            //删除掉自己
            unset($all_parents[0]);

            \Illuminate\Support\Facades\Log::info('testtestetsqqqqq',[$all_parents]);
            if (empty($all_parents)) {
                \Illuminate\Support\Facades\Log::info('直推奖励差集没有找到父级');
                return false;
            }
            $up_id = 0;
            \Illuminate\Support\Facades\Log::info('隔级查找222');
            foreach ($all_parents as $nv) {
                if ($nv['identity_id'] > $self_identity_id) {
                    $up_id = $nv['id'];
                    break;
                }
            }
            if ($up_id == 0) {
                return false;
            }
            $up_user_info = TongUser::query()->where('id', $up_id)->first();
        }
    }


    $key = $up_user_info['identity_id'].'-'.$user_info['identity_id'];
    \Illuminate\Support\Facades\Log::info('销售津贴-key'.$key);
    $recommend = config('user.recommend');
    if(!array_key_exists($key,$recommend)){
        return false;
    }
    //这里根据订单支付的（）
    //获取订单详情
    $order_info = TongOrder::query()->where('id',$order_id)->first();

    if(in_array((int)$order_info['order_price'],[3581,9401,5820])){
        \Illuminate\Support\Facades\Log::info('用户补差升级执行');
        if($order_info['order_price']==3581){
            if($up_user_info['identity_id']==1){
                $result_money = config('user.recommend')['3581']['tiyan'];
            }elseif ($up_user_info['identity_id']==2){
                //品牌会员身份用户升级上来需要吧上次拿到的钱给钱去
                $result_money = config('user.recommend')['3581']['pinpai'];
            }elseif ($up_user_info['identity_id']==3){
                //这里是店铺身份的话用户升级上来需要吧用户上次购买的399奖励的钱给他扣除掉
                $result_money = config('user.recommend')['3581']['dianpu'];
            }elseif ($up_user_info['identity_id']==4){
                //这里是店铺身份的话用户升级上来需要吧用户上次购买的399奖励的钱给他扣除掉
                $result_money = config('user.recommend')['3581']['gongsi'];
            }elseif ($up_user_info['identity_id']==5){
                //这里是店铺身份的话用户升级上来需要吧用户上次购买的399奖励的钱给他扣除掉
                $result_money = config('user.recommend')['3581']['hehuo'];
            }
            \Illuminate\Support\Facades\Log::info('用户补差升级执行-3581-'.$result_money);
        }

        if($order_info['order_price']==9401){
            if($up_user_info['identity_id']==1){
                $result_money = config('user.recommend')['9401']['tiyan'];
            }elseif ($up_user_info['identity_id']==2){
                $result_money = config('user.recommend')['9401']['pinpai'];
            }elseif ($up_user_info['identity_id']==3){
                $result_money = config('user.recommend')['9401']['dianpu'];
            }elseif ($up_user_info['identity_id']==4){
                $result_money = config('user.recommend')['9401']['gongsi'];
            }elseif ($up_user_info['identity_id']==5){
                $result_money = config('user.recommend')['9401']['hehuo'];
            }
            \Illuminate\Support\Facades\Log::info('用户补差升级执行-9401-'.$result_money);
        }


        if($order_info['order_price']==5820){
            if($up_user_info['identity_id']==1){
                $result_money = config('user.recommend')['5820']['tiyan']-400;
            }elseif ($up_user_info['identity_id']==2){
                $result_money = config('user.recommend')['5820']['pinpai']-400;
            }elseif ($up_user_info['identity_id']==3){
                $result_money = config('user.recommend')['5820']['dianpu']-400;
            }elseif ($up_user_info['identity_id']==4){
                $result_money = config('user.recommend')['5820']['gongsi']-400;
            }elseif ($up_user_info['identity_id']==5){
                $result_money = config('user.recommend')['5820']['hehuo']-400;
            }
            \Illuminate\Support\Facades\Log::info('用户补差升级执行-5820-'.$result_money);
        }


    }else{
        $result_money = config('user.recommend')[$key];
    }
    $up_money = $result_money;


    if(config('user.recommend.is_start_ping')){

        \Illuminate\Support\Facades\Log::info("找到推荐的人".$up_user_info['id'].'应分金额：'.$result_money);
        //计算平级的钱
        $ping_money = bcmul($result_money,0.1,2);

        //echo 123;
        //平级奖励
        $ping_res = cultivation_money($up_user_info['id'],$ping_money,'11',$order_info['id']);
        //没有找到平级的人这个钱不用扣除
        if($ping_res!==false){
           // $result_money = bcadd($ping_money,$ping_money);
            $result_money = bcsub($result_money,$ping_money,2);
            \Illuminate\Support\Facades\Log::info("找到了".$up_user_info['id'].'的平级人金额剩余：'.$result_money);
        }
    }
    if(config('user.recommend.is_start_repeat')){
        //开启了复购券奖励
        $repeat_money = bcmul($result_money,0.05,2);
        $result_money = bcsub($result_money,$repeat_money,2);
        //记录复购券
        repeatUserMoney($up_user_info['id'],$repeat_money,'',$order_info['id']);
        \Illuminate\Support\Facades\Log::info("拿出了百分之5作为复购券".$up_user_info['id'].'金额剩余：'.$result_money);
    }
    //var_dump($result_money);
    $insert_data = [];
    //for ($i=0;$i<$order_info['goods_num'];$i++){
//        $auth_store_id = findAuthStore($user_id);
//        if($auth_store_id){
//            //如果找到了认证店铺 并且 $up_user_info['identity_id']==3
//            if($up_user_info['identity_id']==3){
//                //那就把钱给到这个认证店铺 其他正常给他们返钱
//                $up_user_info['id'] = $auth_store_id;
//            }
//
//        }
        $insert_data[] = [
            'u_id'=>$up_user_info['id'],
            'order_id'=>$order_info['id'],
            'money'=>$result_money,
            'type'=>6, //复购津贴
            'symbol'=>1, //收入
            'status'=>2,//直接到账
            'remark'=>'推荐销售津贴到账'.$result_money.'元',
            'created_at'=>date('Y-m-d H:i:s',time()),
            'updated_at'=>date('Y-m-d H:i:s',time())
        ];
    //}
   // var_dump($insert_data);
    //var_dump($insert_data);exit();
    \Illuminate\Support\Facades\Log::info('销售津贴-用户'.$user_info['nickname'].'升级给直推'.$up_user_info['nickname'].$result_money.'元');
  //  TongUser::query()->where('id',$up_user_info['id'])->increment('money',$result_money);
    if($result_money>0){
        \App\Models\TongUserProfit::query()->insert($insert_data);
    }

    //直推奖励的奖励级差
    if($order_info['order_price']==5820){
        $up_money = $up_money+400;
    }
    recommend_money_child_cha($up_user_info['id'],$key,$order_id,$up_money);

}

/**
 * 新增体验会员和品牌会员 找实体店铺进行补助
 * 店铺补助
 * @return false
 */
function store_money($user_id,$order_id){
    //获取用户据信息
    $user_info = TongUser::query()->where('id',$user_id)->first();
    //获取订单信息
    $order_info = TongOrder::query()->where('id',$order_id)->first();
    if(!in_array($user_info['identity_id'],[1,2])){
        return false;
    }
    $user_list = getRedisUserData();
    \Illuminate\Support\Facades\Log::info('店铺补助-新增会员/或品牌会员-进行补助'.'用户'.$user_info['nickname'].'新升等级'.$user_info['identity_id']);
    $all_parents = getAllParents($user_list, $user_info['id']);



    unset($all_parents[0]);
    $all_parents = array_values($all_parents);
    $all_parents = array_filter($all_parents,function($value){
        return $value['identity_id']==3;
    });
    \Illuminate\Support\Facades\Log::info('店铺津贴反润剔除不是店铺的后的',[$all_parents]);
    if(!$all_parents){
        return false;
    }
    $temp_arr_parents = [];
    //数组去除重复
    foreach ($all_parents as $rv){
        if(!array_key_exists($rv['id'],$temp_arr_parents)){
            $temp_arr_parents[$rv['id']] = $rv;
        }
    }
    $all_parents = array_values($temp_arr_parents);

    //var_dump($all_parents);
    $store_ids = array_column($all_parents,'id');
    \Illuminate\Support\Facades\Log::info('店铺补助-新增会员/或品牌会员-找到的所有店铺',$store_ids);
    if(config('user.store_buzu.is_real_store')){
        //
        $store_result = TongUser::query()->whereIn('id',$store_ids)->where('is_real_store',1)->get('id')->toArray();
        $store_result = array_column($store_result,'id');
        $store_ids = array_intersect_key($store_ids,$store_result);
        $store_ids = array_values($store_ids);
        if(!$store_ids){
            return false;
        }
        if(config('user.store_buzu.find')=='first'){
            $store_id =reset($store_ids);
        }elseif (config('user.store_buzu.find')=='end'){
            $store_id =end($store_ids);
        }else{
            $store_id = $store_ids[array_rand($store_ids)];
        }
        //var_dump($store_id);
        $money = ($user_info['identity_id']==1) ? config('user.store_buzu.tiyan'):config('user.store_buzu.pinpai');

        if ((int)$order_info['order_price']==3980 || (int)$order_info['order_price']==3581){
            $money = $money-40;
        }

        if(config('user.store_buzhu.is_start_ping')){

            //计算平级的钱
            $ping_money = bcmul($money,0.1,2);

            //平级奖励
            $ping_res = cultivation_money($store_id,$ping_money);
            //没有找到平级的人这个钱不用扣除
            if($ping_res!==false){
                $money = bcsub($money,$ping_money,2);
            }
        }

        if(config('user.store_buzhu.is_start_repeat')){
            //开启了复购券奖励
            $repeat_money = bcmul($money,0.05,2);
            $money = bcsub($money,$repeat_money,2);
            //记录复购券
            repeatUserMoney($store_id,$repeat_money);
        }

        $insert_data = [
            'u_id'=>$store_id,
            'money'=>$money,
            'type'=>6,//门店津贴店补
            'symbol'=>1,
            'status'=>2,//直接到账
            'remark'=>'门店津贴店补'.$money,
            'created_at'=>date('Y-m-d H:i:s',time()),
            'updated_at'=>date('Y-m-d H:i:s',time())
        ];
        \App\Models\TongUserProfit::query()->insert($insert_data);
        \Illuminate\Support\Facades\Log::info('店铺补助-新增会员/或品牌会员-进行补助(实体商户)'.'用户'.$user_info['nickname'].'新升等级'.$user_info['identity_id'].'给'.$store_id.'商户-'.$money.'元');
    }else{
        if(!$store_ids){
            return false;
        }
        if(config('user.store_buzu.find')=='first'){
            $store_id =reset($store_ids);
        }elseif (config('user.store_buzu.find')=='end'){
            $store_id =end($store_ids);
        }else{
            $store_id = $store_ids[array_rand($store_ids)];
        }
        //var_dump($store_id);

        $money = ($user_info['identity_id']==1) ? config('user.store_buzu.tiyan'):config('user.store_buzu.pinpai');

        if ((int)$order_info['order_price']==3980 || (int)$order_info['order_price']==3581){
            //判断用户有没有399的订单
            $is_399_query = TongOrder::query()->where('u_id',$user_info['id'])
                ->whereIn('status',[1,3,4,5])->where('goods_price',399);
            if($is_399_query->exists()){
                $money = $money-40;
            }

        }
        if(config('user.store_buzhu.is_start_ping')){

            //计算平级的钱
            $ping_money = bcmul($money,0.1,2);
            //$money = bcsub($money,$ping_money,2);
            //平级奖励
            $ping_res = cultivation_money($store_id,$ping_money);
            if($ping_res!==false){
                $money = bcsub($money,$ping_money,2);
            }
        }


        if(config('user.store_buzhu.is_start_repeat')){
            //开启了复购券奖励
            $repeat_money = bcmul($money,0.05,2);
            $money = bcsub($money,$repeat_money,2);
            //记录复购券
            repeatUserMoney($store_id,$repeat_money);
        }

        $insert_data = [
            'u_id'=>$store_id,
            'money'=>$money,
            'type'=>6,//门店津贴店补
            'symbol'=>1,
            'status'=>2,//直接到账
            'remark'=>'门店津贴店补'.$money,
            'created_at'=>date('Y-m-d H:i:s',time()),
            'updated_at'=>date('Y-m-d H:i:s',time())
        ];
        \App\Models\TongUserProfit::query()->insert($insert_data);
        \Illuminate\Support\Facades\Log::info('店铺补助-新增会员/或品牌会员-进行补助(不分实体或虚拟)'.'用户'.$user_info['nickname'].'新升等级'.$user_info['identity_id'].'给'.$store_id.'商户-'.$money.'元');
    }

}

/**
 * 111
 * 订单的区域津贴
 * @return true | false
 */
function area_money($user_id,$order_id){
    \Illuminate\Support\Facades\Log::info('执行区域津贴');
    //获取用户的信息
    $user_info = TongUser::query()->where('id',$user_id)->first();
    \Illuminate\Support\Facades\Log::info('执行区域津贴aa');
    //获取订单的详情
    $order_query = TongOrder::query()->where('u_id',$user_id)->where('id',$order_id)->where('status',1);
    \Illuminate\Support\Facades\Log::info('执行区域津贴1');
    if(!$order_query->exists()){
        return false;
    }
    \Illuminate\Support\Facades\Log::info('执行区域津贴2');
    $order_info = $order_query->first();
//    if(((int)$order_info['goods_price']!=399) || ((int)$order_info['goods_price']!=3980)){
//        \Illuminate\Support\Facades\Log::info('执行区域津贴3'.(int)$order_info['goods_price']);
//        return false;
//    }
    if(!in_array((int)$order_info['goods_price'],[399,3980])){
        return false;
    }
    \Illuminate\Support\Facades\Log::info('执行区域津贴4');
    \Illuminate\Support\Facades\Log::info('执行区域津贴-购买的商品符合');
    //只有规定的会员才会进行区域津贴
    if(!in_array($user_info['identity_id'],config('user.area_money.user_identity_id'))){
        \Illuminate\Support\Facades\Log::info('执行区域津贴5');
        return false;
    }
    \Illuminate\Support\Facades\Log::info('执行区域津贴6');
    //
    $order_399_num = TongOrder::query()->where('u_id',$user_id)
        ->where('goods_price',399)->whereIn('status',[1,3,4,5])->count();
    $order_3980_num = TongOrder::query()->where('u_id',$user_id)
        ->where('goods_price',3980)->whereIn('status',[1,3,4,5])->count();
    //查找上级
    $user_list = getRedisUserData();
    $all_parents = getAllParents($user_list, $user_info['id']);
    \Illuminate\Support\Facades\Log::info('执行区域津贴7',$order_info['order_address_info']);


    unset($all_parents[0]);
    $all_parents = array_values($all_parents);
    //查找上级是公司或合伙人身份的人
    $all_parents = array_filter($all_parents,function($value){
        return $value['identity_id']== 4 or $value['identity_id']==5;
    });
    if(!$all_parents){
        return false;
    }
    $temp_arr_parents = [];
    //数组去除重复
    foreach ($all_parents as $rv){

        if(!array_key_exists($rv['id'],$temp_arr_parents)){
            $temp_arr_parents[$rv['id']] = $rv;
        }
    }
    $all_parents = array_values($temp_arr_parents);

//    if(config('user.area_money.is_real')){
//        //只对实体进行反润
//        return true;
//    }
    //找出上级的公司
    $parent_company = array_filter($all_parents,function($value){
        return $value['identity_id']== 4;
    });
    //\Illuminate\Support\Facades\Log::info('');

    if($parent_company){
        //return false;
        \Illuminate\Support\Facades\Log::info('执行区域津贴到了这里',$parent_company);
        // \Illuminate\Support\Facades\Log::info('执行区域津贴到了这里');
        $company_ids = array_column($parent_company,'id');
        \Illuminate\Support\Facades\Log::info('执行区域津贴到了这里',$company_ids);
        //查询数据库找出符合的公司

        $order_info_address = $order_info['order_address_info'];

        $company_res = TongUser::query()->whereIn('id',$company_ids)
            ->where('area_id',$order_info_address['area_id'])->pluck('id')->toArray();
        if($company_res){
            //如果没有找到合适的公司就给他直接返回
            \Illuminate\Support\Facades\Log::info('没有找到合适的公司给他们区域奖励',$company_res);
            //return false;
            //获取数组的交集接着往下给他们分钱
            $company_ids = array_intersect($company_ids,$company_res);

            if(config('user.area_money.company.find')=='first'){
                $company_id =reset($company_ids);
            }elseif (config('user.area_money.company.find')=='end'){
                $company_id =end($company_ids);
            }else{
                $company_id = $company_ids[array_rand($company_ids)];
            }
            $money = 0;
            if($order_info['goods_price']==399){
                if($order_399_num==1){
                    \Illuminate\Support\Facades\Log::info('公司区域奖励399首单');
                    $money = config('user.area_money.company.399.first_order');
                }elseif($order_399_num>1){
                    \Illuminate\Support\Facades\Log::info('公司区域奖励399非首单');
                    $money = config('user.area_money.company.399.other_order');
                }
            }else{
                if($order_3980_num==1){
                    \Illuminate\Support\Facades\Log::info('公司区域奖励3980首单');
                    $money = config('user.area_money.company.3980.first_order');
                }elseif($order_3980_num>1){
                    \Illuminate\Support\Facades\Log::info('公司区域奖励3980非首单');
                    $money = config('user.area_money.company.3980.other_order');
                }
            }

            if(config('user.store_buzhu.is_start_ping')){

                //计算平级的钱
                $ping_money = bcmul($money,0.1,2);
                $money = bcsub($money,$ping_money,2);
                //平级奖励
                $ping_res = cultivation_money($company_id,$ping_money,'区域',$order_info['id'],14);
                if($ping_res!==false){
                    $money = bcsub($money,$ping_money,2);
                }
            }

            if(config('user.store_buzhu.is_start_repeat')){
                //开启了复购券奖励
                $repeat_money = bcmul($money,0.05,2);
                $money = bcsub($money,$repeat_money,2);
                //记录复购券
                repeatUserMoney($company_id,$repeat_money,'区域',$order_info['id'],14);
            }

            $insert_data = [
                'u_id'=>$company_id,
                'money'=>$money,
                'order_id'=>$order_info['id'],
                'type'=>6,//门店津贴店补
                'symbol'=>1,
                'status'=>2,//直接到账
                'remark'=>'区域津贴补助'.$money,
                'created_at'=>date('Y-m-d H:i:s',time()),
                'updated_at'=>date('Y-m-d H:i:s',time())
            ];
            \App\Models\TongUserProfit::query()->insert($insert_data);
        }
    }



    //找出上级的合伙人
    $parent_hehuo  = array_filter($all_parents,function($value){
        return $value['identity_id']== 5;
    });
    if($parent_hehuo){
        //return false;
        $order_info_address = $order_info['order_address_info'];

        $hehuo_ids = array_column($parent_hehuo,'id');

        \Illuminate\Support\Facades\Log::info('区域奖励执行到了这里8');

        $hehuo_res = TongUser::query()->whereIn('id',$hehuo_ids)
            ->where('city_id',$order_info_address['city_id'])->pluck('id')->toArray();
        if($hehuo_res){
            //如果没有找到合适的公司就给他直接返回
            \Illuminate\Support\Facades\Log::info('没有找到合适的合伙人给他们区域奖励');
            //return false;
            //获取数组的交集接着往下给他们分钱
            $hehuo_ids = array_intersect($hehuo_ids,$hehuo_res);




            if(config('user.area_money.partner.find')=='first'){
                $hehuo_id =reset($hehuo_ids);
            }elseif (config('user.area_money.partner.find')=='end'){
                $hehuo_id =end($hehuo_ids);
            }else{
                $hehuo_id = $hehuo_ids[array_rand($hehuo_ids)];
            }
            $money = 0;
            if($order_info['goods_price']==399){
                if($order_399_num==1){
                    \Illuminate\Support\Facades\Log::info('合伙人区域奖励399首单');
                    $money = config('user.area_money.partner.399.first_order');
                }elseif($order_399_num>1){
                    \Illuminate\Support\Facades\Log::info('合伙人区域奖励399非首单');
                    $money = config('user.area_money.partner.399.other_order');
                }
            }else{
                if($order_3980_num==1){
                    \Illuminate\Support\Facades\Log::info('合伙人区域奖励3980首单');
                    $money = config('user.area_money.partner.3980.first_order');
                }elseif($order_3980_num>1){
                    \Illuminate\Support\Facades\Log::info('合伙人区域奖励3980非首单');
                    $money = config('user.area_money.partner.3980.other_order');
                }
            }

            if(config('user.store_buzhu.is_start_ping')){

                //计算平级的钱
                $ping_money = bcmul($money,0.1,2);
                $money = bcsub($money,$ping_money,2);
                //平级奖励
                $ping_res = cultivation_money($hehuo_id,$ping_money,'区域',$order_info['id'],14);
                if($ping_res!==false){
                    $money = bcsub($money,$ping_money,2);
                }
            }

            if(config('user.store_buzhu.is_start_repeat')){
                //开启了复购券奖励
                $repeat_money = bcmul($money,0.05,2);
                $money = bcsub($money,$repeat_money,2);
                //记录复购券
                repeatUserMoney($company_id,$repeat_money,'区域',$order_info['id'],14);
            }

            $insert_data = [
                'u_id'=>$hehuo_id,
                'money'=>$money,
                'type'=>6,//门店津贴店补
                'symbol'=>1,
                'order_id'=>$order_info['id'],
                'status'=>2,//直接到账
                'remark'=>'区域津贴补助'.$money,
                'created_at'=>date('Y-m-d H:i:s',time()),
                'updated_at'=>date('Y-m-d H:i:s',time())
            ];
            \App\Models\TongUserProfit::query()->insert($insert_data);
        }

    }

}

/**
 *培育津贴（平级奖励）
 * @return false
 */
function cultivation_money($user_id,$money,$prefix='',$order_id=0,$type=7){
    //echo 123;
    $user_list = getRedisUserData();
    $all_parents = getAllParents($user_list, $user_id);

    \Illuminate\Support\Facades\Log::info('执行平级奖励'.$user_id);

    if(!$all_parents){
        return false;
    }
    \Illuminate\Support\Facades\Log::info('平级查询',$all_parents);
    $temp_arr_parents = [];
    //数组去除重复
    foreach ($all_parents as $rv){
        if(!array_key_exists($rv['id'],$temp_arr_parents)){
            $temp_arr_parents[$rv['id']] = $rv;
        }
    }
    $all_parents = array_values($temp_arr_parents);



    $self_identity_id = $all_parents[0]['identity_id'];
    unset($all_parents[0]);
    $all_parents = array_values($all_parents);

    \Illuminate\Support\Facades\Log::info($user_id.'自身等级'.$self_identity_id);
    \Illuminate\Support\Facades\Log::info('平级获取到的上级'.$user_id,[$all_parents]);

    //查找上级是公司或合伙人身份的人
    $all_parents = array_filter($all_parents,function($value) use ($self_identity_id){
        return $value['identity_id']== $self_identity_id;
    });
    \Illuminate\Support\Facades\Log::info('平级获取到的结果'.$user_id,$all_parents);
    //找到上级和自己身份相同的人
    if(!$all_parents){
        return false;
    }
    $all_parents = array_values($all_parents);
    //var_dump($all_parents);
    if(config('user.cultivation.find')=='first'){
        $p_id =reset($all_parents);
    }elseif (config('user.cultivation.find')=='end'){
        $p_id =end($all_parents);
    }else{
        $p_id = $all_parents[array_rand($all_parents)];
    }
    $insert_data = [];
    if($money>0){
        //给这个人
        $insert_data = [
            'u_id'=>$p_id['id'],
            'money'=>$money,
            'type'=>$type,//培育津贴 （平级奖励）
            'symbol'=>1,
            'status'=>2,//直接到账
            'order_id'=>$order_id,
            //'remark'=>'区域津贴补助'.$money,
            'remark'=>$prefix.'津贴（平级收益）补助'.$money,
            'created_at'=>date('Y-m-d H:i:s',time()),
            'updated_at'=>date('Y-m-d H:i:s',time())
        ];
        \App\Models\TongUserProfit::query()->insert($insert_data);
    }

    return $p_id;
}


/**
 *
 * 用户下单差价升级
 * @return true|false
 */
function price_difference_upgrade($user_id,$order_id)
{
    //获取订单的信息
    //$order_info = TongOrder::query()->where('id',$order_id)->first();
    //获取用户信息
    $user_info = TongUser::query()->where('id', $user_id)->first();
    \Illuminate\Support\Facades\Log::info('用户补差价升级',['当前身份'.$user_info['identity_id']]);
    if (!in_array($user_info['identity_id'], config('user.chajia.price_difference_identity'))) {
        return false;
    }

    //获取会员用户当前已经花过多少钱了
    $user_order_money = TongOrder::query()->where('u_id', $user_id)->whereIn('status', [1, 3, 4, 5])->sum('all_order_price');
    \Illuminate\Support\Facades\Log::info('用户补差价升级',['当前身份'.$user_info['identity_id'],'订单金额'.$user_order_money]);
    if($user_order_money >= config('user.chajia.jiankangguan')){
        \Illuminate\Support\Facades\Log::info('11111111111111111111111111111111111');
        $res_query = TongOrder::query()->where('u_id', $user_id)->where('goods_price','39800');
        if($res_query->exists()) {
            TongUser::query()->where('id', $user_id)->update(['identity_id' => 4]);
            updateUserIdentity($user_id, 4);
            \Illuminate\Support\Facades\Log::info('用户补差价升级,1调用：recommend_money');
            new_user_recommend($user_id, $order_id);
            //new_store_bu($user_id, $order_id);
            //store_money_tow($user_id, $order_id);
            store_money_two($user_id, $order_id);
        }
    }
    if($user_order_money >= config('user.chajia.dianpu')) {
        \Illuminate\Support\Facades\Log::info('2222222222222222222222222222222222');
        //平且用户支付过9800的订单
        $res_query = TongOrder::query()->where('u_id', $user_id)->where('goods_price','9800');
        if($res_query->exists()) {
            shareReward($user_id,$order_id,700);

            if ($user_info['identity_id'] == 2) {
                \Illuminate\Support\Facades\Log::info('用户补差价升级', ['当前身份' . $user_info['identity_id'], '订单金额' . $user_order_money, '升级到店铺']);
                TongUser::query()->where('id', $user_id)->update(['identity_id' => 3]);
                updateUserIdentity($user_id, 3);
                \Illuminate\Support\Facades\Log::info('用户补差价升级,1调用：recommend_money');
                new_user_recommend($user_id, $order_id);
                //new_store_bu($user_id, $order_id);
                //升级后调用直推奖励给上级用户
                //recommend_money($user_id, $order_id);
                //极差 (用户升级到了店铺)
                //grade_difference($user_info['id']);
                store_money_two($user_id, $order_id);
                return true;
            }
            if ($user_info['identity_id'] == 1) {
                \Illuminate\Support\Facades\Log::info('用户补差价升级', ['当前身份' . $user_info['identity_id'], '订单金额' . $user_order_money, '升级到店铺']);
                TongUser::query()->where('id', $user_id)->update(['identity_id' => 3]);
                updateUserIdentity($user_id, 3);
                \Illuminate\Support\Facades\Log::info('用户补差价升级,1调用：recommend_money');
                new_user_recommend($user_id, $order_id);
                //new_store_bu($user_id, $order_id);
                store_money_two($user_id, $order_id);
                //升级后调用直推奖励给上级用户
                //recommend_money($user_id, $order_id);
                //极差 (用户升级到了店铺)
                //grade_difference($user_info['id']);
                return true;
            }
            if ($user_info['identity_id'] == 0) {
                \Illuminate\Support\Facades\Log::info('用户补差价升级', ['当前身份' . $user_info['identity_id'], '订单金额' . $user_order_money, '升级到店铺']);
                TongUser::query()->where('id', $user_id)->update(['identity_id' => 3]);
                updateUserIdentity($user_id, 3);
                \Illuminate\Support\Facades\Log::info('用户补差价升级,1调用：recommend_money');
                new_user_recommend($user_id, $order_id);
                store_money_two($user_id, $order_id);
                //new_store_bu($user_id, $order_id);
                //升级后调用直推奖励给上级用户
                //recommend_money($user_id, $order_id);
                //极差 (用户升级到了店铺)
                //grade_difference($user_info['id']);
                return true;
            }
        }
    }
    if ($user_order_money >= config('user.chajia.pinpai')) {
        \Illuminate\Support\Facades\Log::info('333333333333333333333333333333333333333333333333');
        \Illuminate\Support\Facades\Log::info('执行3980');
        //平且用户支付过9800的订单
        $res_query = TongOrder::query()->where('u_id', $user_id)->where('goods_price','3980');
        if($res_query->exists()) {
            shareReward($user_id,$order_id,300);
            if ($user_info['identity_id'] == 1) {
                \Illuminate\Support\Facades\Log::info('用户补差价升级', ['当前身份' . $user_info['identity_id'], '订单金额' . $user_order_money, '升级到品牌']);
                TongUser::query()->where('id', $user_id)->update(['identity_id' => 2]);
                updateUserIdentity($user_id, 2);
                \Illuminate\Support\Facades\Log::info('用户补差价升级,2调用：recommend_money');
                new_user_recommend($user_id, $order_id);
                //new_store_bu($user_id, $order_id);
                store_money_two($user_id, $order_id);
                //升级后调用直推奖励给上级用户
                //recommend_money($user_id, $order_id);
                //给的门店津贴店补
                //store_money($user_id, $order_id);
                //极差 (用户升级到了品牌)
               // grade_difference($user_info['id']);
                return true;
            }
            if ($user_info['identity_id'] == 0) {
                \Illuminate\Support\Facades\Log::info('用户补差价升级', ['当前身份' . $user_info['identity_id'], '订单金额' . $user_order_money, '升级到品牌']);
                TongUser::query()->where('id', $user_id)->update(['identity_id' => 2]);
                updateUserIdentity($user_id, 2);
                \Illuminate\Support\Facades\Log::info('用户补差价升级,2调用：recommend_money');
                new_user_recommend($user_id, $order_id);
                store_money_two($user_id, $order_id);
                //new_store_bu($user_id, $order_id);
                //升级后调用直推奖励给上级用户
               // recommend_money($user_id, $order_id);
                //给的门店津贴店补
                //store_money($user_id, $order_id);
                //极差 (用户升级到了品牌)
               // grade_difference($user_info['id']);
                return true;
            }
        }
    }
    if ($user_order_money >= config('user.chajia.tiyan')) {
        \Illuminate\Support\Facades\Log::info('444444444444444444444444444444444444444444444444444');
        if($user_info['identity_id']==0){
            \Illuminate\Support\Facades\Log::info('用户补差价升级',['当前身份'.$user_info['identity_id'],'订单金额'.$user_order_money,'升级到体验会员']);
            TongUser::query()->where('id', $user_id)->update(['identity_id' => 1]);
            updateUserIdentity($user_id,1);
            new_user_recommend($user_id, $order_id);
            store_money_two($user_id, $order_id);
            //升级后调用直推奖励给上级用户
            //recommend_money($user_id,$order_id);
            //给的门店津贴店补
            //store_money($user_id,$order_id);
            return true;
        }
    }
}

/**
 * 修改用户的缓存身份信息
 * @return void
 */
function updateUserIdentity_old($user_id,$identity_id){
    $user_list = getRedisUserData();
    $key = array_search($user_id, array_column($user_list, 'id'));
    $user_list[$key]['identity_id'] = $identity_id;
    $key = config('user.add_redis_data_key');
    Cache::store('redis')->put($key,$user_list);
}

/**
 * @param $user_id
 * @param $identity_id
 * @return void
 * @throws \Psr\SimpleCache\InvalidArgumentException
 */
function updateUserIdentity($user_id,$identity_id){
    //Redis::connection()->client()->hGet('user_list','user_p78');
    $user_info = RedisService::hGet($user_id);
    $user_info['identity_id'] = $identity_id;
    RedisService::hUpdate($user_id,$user_info);
}




function updateUserRealStore_old($user_id,$is_real_store){
    $user_list = getRedisUserData();
    $key = array_search($user_id, array_column($user_list, 'id'));
    $user_list[$key]['is_real_store'] = $is_real_store;
    $key = config('user.add_redis_data_key');
    Cache::store('redis')->put($key,$user_list);
}

/**
 * @param $user_id
 * @param $is_real_store
 * @return void
 * @throws RedisException
 */
function updateUserRealStore($user_id,$is_real_store){
    $user_info = RedisService::hGet($user_id);
    $user_info['is_real_store'] = $is_real_store;
    RedisService::hUpdate($user_id,$user_info);
}

/**
 * 计算会员的极差
 * 传入升级到体验店或品牌会员的id
 * @return false
 */
function grade_difference($user_id){
    return false;
    if(!config('user.grade_difference.user_identity_id')){
        \Illuminate\Support\Facades\Log::info('极差奖励,没有开启');
    }
    //获取用户信息
    $user_info = TongUser::query()->where('id',$user_id)->first();
    if(!in_array($user_info['identity_id'],config('user.grade_difference.user_identity_id'))){
        return false;
    }
    //获取所有的人
    $user_list = getRedisUserData();
    $all_parents = getAllParents($user_list, $user_id);
    //var_dump($all_parents);
    if(!$all_parents){
        return false;
    }
    $temp_arr_parents = [];
    //数组去除重复
    foreach ($all_parents as $rv){
        if(!array_key_exists($rv['id'],$temp_arr_parents)){
            $temp_arr_parents[$rv['id']] = $rv;
        }
    }
    $all_parents = array_values($temp_arr_parents);

    $self_p_id = $all_parents[0]['p_id'];
    //获取自己的推荐人
    $tuijian_arr= array_filter($all_parents,function($value) use ($self_p_id){
        return $value['id'] == $self_p_id;
    });
    $tuijian_arr = array_values($tuijian_arr);
    if($user_info['identity_id']==3){
        $tiaojian = config('user.grade_difference.give_user');
    }else{
        $tiaojian = [1];
    }

    //判断推荐人的身份是否符合
    if(!in_array($tuijian_arr[0]['identity_id'],$tiaojian)){
        \Illuminate\Support\Facades\Log::info('极差奖励,直推的人条件身份不符合用户id'.$tuijian_arr[0]['id'].'身份id'.$tuijian_arr[0]['identity_id']);
        //身份不是体验会员或品牌会员
        return false;
    }
    $self_identity_id = $all_parents[0]['identity_id'];
    $all_parents = array_values($all_parents);


    //向上查找到店铺的身份
    $all_parents = array_filter($all_parents,function($values) use($self_identity_id){
        return $values['identity_id']== 3;
    });

    //给直推人的极差奖励

    if(!$all_parents){
        \Illuminate\Support\Facades\Log::info('极差奖励,向上没有找到店铺');
        return false;
    }

    $all_parents = array_values($all_parents);
    //var_dump($all_parents);exit();
    $money1 = $tuijian_arr[0]['identity_id']==1? 100:2940;
    $insert_data = [];
    $insert_data[] = [
        'u_id'=>$tuijian_arr[0]['id'],
        'money'=>$money1,
        'type'=>8,//门店津贴极差
        'symbol'=>1,
        'status'=>2,//直接到账
        'remark'=>'体验店店补极差奖励'.$money1,
        'created_at'=>date('Y-m-d H:i:s',time()),
        'updated_at'=>date('Y-m-d H:i:s',time())
    ];
    $money2 = 0.00;
    if($self_identity_id==3 && $tuijian_arr[0]['identity_id']==1){
        $money2 = 3330;
    }elseif ($self_identity_id==3 && $tuijian_arr[0]['identity_id']==2){
        $money2 = 490;
    }elseif ($self_identity_id==2 && $tuijian_arr[0]['identity_id']==1){
        $money2 = 1290;
    }
    //给店铺
    $insert_data[] = [
        'u_id'=>$all_parents[0]['id'],
        'money'=>$money2,
        'type'=>8,//门店津贴极差
        'symbol'=>1,
        'status'=>2,//直接到账
        'remark'=>'体验店店补极差奖励'.$money2,
        'created_at'=>date('Y-m-d H:i:s',time()),
        'updated_at'=>date('Y-m-d H:i:s',time())
    ];
    \App\Models\TongUserProfit::query()->insert($insert_data);





}

/**
 * 记录用户的复购券的金额
 * @return void
 */
function repeatUserMoney($u_id,$money,$prefix = '',$order_id = 0,$type=10){
    $order_info = TongOrder::query()->where('id',$order_id)->first();
    // 启用事务保证数据的统一性
    \Illuminate\Support\Facades\DB::beginTransaction();
    try{
        if($money>0){
            $insert_data[] = [
                'u_id'=>$u_id,
                'money'=>$money,
                'order_id'=>$order_id,
                'type'=>$type,//获取到的复购券，
                'status'=>2,
                'symbol'=>1,
                'remark'=>$prefix.'提货券收益'.$money,
                'created_at'=>date('Y-m-d H:i:s',time()),
                'updated_at'=>date('Y-m-d H:i:s',time())
            ];
            if(config("test.test_start")==1 && in_array($order_info['u_id'],explode(',',str_replace('，', ',', config("test.test_user"))))){
                \App\Models\TongUserProfitTest::query()->insert($insert_data);
            }else{
                \App\Models\TongUserProfit::query()->insert($insert_data);
            }

        }

        //增加用户的
       // TongUser::query()->where('id',$u_id)->increment('repeat_money',$money);
        \Illuminate\Support\Facades\DB::commit();
    }catch (Exception $e){
        \Illuminate\Support\Facades\DB::rollBack();
        throw new DaoException(ResponseEnum::HTTP_ERROR,$e->getMessage());
    }

}

/**
 * 判断用户会员是否是首单
 * @return false
 */
function isFirstOrder($user_id,$g_id,&$data){
    //dd($user_id);
    $user_info = TongUser::query()->where('id',$user_id)->first();

    if(!in_array($user_info['identity_id'],config('user.chajia.price_difference_identity'))){
        //如果不是这几个身份直接返回不是首单
        return false;
    }
    $goods_query = \App\Models\TongGood::query()->where('is_shop_goods',0)->where('id',$g_id);
    if(!$goods_query->exists()){
        // 不是会员商品
        return false;
    }
    $goods_info = $goods_query->first();
    if($user_info['identity_id']==1){
        $key = 'tiyan';
        $goods_price_key = 'experience_member_price';
    }elseif ($user_info['identity_id']==2){
        $key = 'pinpai';
        $goods_price_key = 'brand_member_price';
    }
    $price = config('user.chajia')[$key];

    $order_query = TongOrder::query()->where('goods_price',$price)
        ->where('u_id',$user_id)
        ->where('status','!=',1);
    if($order_query->exists() && ($goods_info[$goods_price_key]==config('user.chajia.pinpai')) && $user_info['identity_id']==1){
        $data['order_price'] = config('user.chajia.pinpai')-config('user.chajia.tiyan');
    }elseif ($order_query->exists() && ($goods_info[$goods_price_key]==config('user.chajia.pinpai')) && $user_info['identity_id']==2){
        $data['order_price'] = config('user.chajia.dianpu')-config('user.chajia.pinpai');
    }
}



function array_unique_by_key($array, $key) {
    $unique = array();
    foreach ($array as $value) {
        if (!isset($unique[$value[$key]])) {
            $unique[$value[$key]] = $value;
        }
    }
    return array_values($unique);
}

/**
 * 如果下一个元素的某个值比上一个小的话直接剔除
 * @param $array
 * @return array
 */

function removeSmallerNeighbors($array, $key) {
    if (empty($array) || !is_array($array)) {
        return $array; // 数组为空或不是数组，直接返回
    }

    for ($i = 1; $i < count($array); $i++) {
        // 检查前一个元素是否存在且包含指定的键
        if (!isset($array[$i - 1][$key]) || !isset($array[$i][$key])) {
            continue; // 跳过不包含所需键的元素
        }

        if ($array[$i][$key] < $array[$i - 1][$key]) {
            unset($array[$i]);
            // 注意：移除元素后，数组的索引会发生变化
            // 如果需要，可以调整$i的值或改变循环逻辑
            $i--; // 可选：如果希望连续比较相邻元素，可以递减$i
        }
    }

    $array = array_values($array); // 重新索引数组
    return $array;
}

/**
 * 判断用户是否是小推大
 * @return true|false
 */
function isSmallToBig($order_id)
{
    //获取订单信息
    $order_info = TongOrder::query()->where('id',$order_id)->first();
    \Illuminate\Support\Facades\Log::info('isSmallToBig1');
    //获取到用户 的信息
    $user_info = TongUser::query()->where('id',$order_info['u_id'])->first();
    \Illuminate\Support\Facades\Log::info('isSmallToBig2');
    //查询自己的直推人是否是小推大
    if($user_info['p_id']==0){
        \Illuminate\Support\Facades\Log::info('isSmallToBig3');
        return false;
    }
    //获取自己的上级
    $up_user_info = TongUser::query()->where('id',$user_info['p_id'])->first();
    \Illuminate\Support\Facades\Log::info('isSmallToBig4');
    //推荐人的身份是普通用户不用管他
//    if($user_info['identity_id']==0){
//        \Illuminate\Support\Facades\Log::info('isSmallToBig5');
//        return false;
//    }
    //判断用户的订单是否是
    $goods_price = (int)$order_info['order_price'];
    \Illuminate\Support\Facades\Log::info('isSmallToBig6');
    if(in_array($goods_price,[3980,9800,5820]) && in_array($up_user_info['identity_id'],[1,2])){
        if($up_user_info['identity_id']==2 && $goods_price==3980){
            return false;
        }
        $user_list = getRedisUserData();
        $all_parents = getAllParents($user_list, $user_info['id']);
        \Illuminate\Support\Facades\Log::info('isSmallToBig7',[$all_parents]);
        $temp_arr_parents = [];
        if(!empty($all_parents)){
            //数组去除重复
            foreach ($all_parents as $rv){
                $temp_arr_parents[$rv['id']] = $rv;
            }
            $all_parents = array_values($temp_arr_parents);
        }


        unset($all_parents[0]);
        \Illuminate\Support\Facades\Log::info('isSmallToBig8');
        $all_parents = array_values($all_parents);

        //找出指定的店铺
        $all_parents = array_filter($all_parents,function($parents_val) use ($user_info){
            return $parents_val['is_real_store']==1;
        });
        if(!$all_parents){
            //没找到指定店铺
            return false;
        }else{
            //找到了指定店铺
            return true;
        }
    }else{
        \Illuminate\Support\Facades\Log::info('isSmallToBig8');
        return false;
    }


}

/**
 * @param $user_id
 * @param $parentList
 * @return false|mixed
 * @throws \Psr\SimpleCache\InvalidArgumentException
 */
function findAuthStore($user_id)
{

    $user_list = getRedisUserData();
    $all_parents = getAllParents($user_list, $user_id);
    //找出用户身份比自己等级高的
    if(empty($all_parents)){
        \Illuminate\Support\Facades\Log::info('直推奖励差集没有找到父级');
        return false;
    }

    $temp_arr_parents = [];
    //数组去除重复
    foreach ($all_parents as $rv){
        if(!array_key_exists($rv['id'],$temp_arr_parents)){
            $temp_arr_parents[$rv['id']] = $rv;
        }
    }
    $all_parents = array_values($temp_arr_parents);

    unset($all_parents[0]);
    $all_parents = array_values($all_parents);
    //过滤出来是指定店铺的人
    //找出指定的店铺
    $all_parents = array_filter($all_parents,function($parents_val){
        return $parents_val['is_real_store']==1;
    });
    $all_parents = array_values($all_parents);
    \Illuminate\Support\Facades\Log::info('执行查找指定商店',$all_parents);
    if(!$all_parents){
        //没找到指定店铺
        return false;
    }else{
        \Illuminate\Support\Facades\Log::info($all_parents[0]);
        //找到了指定店铺
        return $all_parents[0]['id'];
    }

}

/**
 * 另外一套的补差逻辑
 * @return false
 */
function price_difference_upgrade_tow($user_id,$order_id){
    \Illuminate\Support\Facades\Log::info('我执行到了这里。。。。。。');
    //获取订单信息
    $order_info = TongOrder::query()->where('id',$order_id)->first();
    //获取到用户 的信息
    $user_info = TongUser::query()->where('id',$user_id)->first();
    //查询自己的直推人是否是小推大
    if($user_info['p_id']==0){
        return false;
    }
    //获取所有的用户信息
    $user_list = getRedisUserData();
    $all_parents = getAllParents($user_list, $user_info['id']);

    $temp_arr_parents = [];
    if(!empty($all_parents)){
        //数组去除重复
        foreach ($all_parents as $rv){
            $temp_arr_parents[$rv['id']] = $rv;
        }
        $all_parents = array_values($temp_arr_parents);
    }

    \Illuminate\Support\Facades\Log::info('我执行到了这里111111111111111。。。。。。');
    unset($all_parents[0]);
    $all_parents = array_values($all_parents);

    //找出自己的直接推荐人
    $p_info = $all_parents[0];
    //var_dump($p_info);
    //找出指定的店铺
    $store_arr = array_filter($all_parents,function($parents_val) use ($user_info){
        return $parents_val['is_real_store']==1;
    });
    $store_arr = array_values($store_arr);
    //dd($all_parents);
    $store_info = $store_arr[0];
    \Illuminate\Support\Facades\Log::info('我执行到了这里222222。。。。。。',[$store_info['id']]);

    //向上找出公司或合伙人
    $store_up_parents = array_filter($all_parents,function($item){
        return $item['identity_id']>=4;
    });

    $store_up_parents = array_values($store_up_parents);
    //数组的查找



    $store_up_parents = removeSmallerNeighbors($store_up_parents,'identity_id');
    \Illuminate\Support\Facades\Log::info('我执行到了这里222222。。。。。。',[$store_up_parents]);
    //array_unshift($all_parents,$self_arr);
    $store_up_parents = array_values($store_up_parents);

    //if($)
//    foreach ($store_up_parents as  $k => $v){
//        if($v['id']!=$store_info['id']){
//            unset($store_up_parents[$k]);
//        }else{
//            break;
//        }
//    }
    $store_up_parents = array_unique_by_key($store_up_parents,'identity_id');

    $store_up_parents = array_values($store_up_parents);

    \Illuminate\Support\Facades\Log::info('我执行到了这里33333。。。。。。',[$store_up_parents]);




    $money1 = $p_info['identity_id']==1? 100:2940;
    if((int)$order_info['order_price']==5820){
        $money1 = 0;
    }





    $insert_data = [];
    if($money1>0){

        //计算平级的钱
        $ping_money = bcmul($money1,0.1,2);

        //echo 123;
        //平级奖励
        $ping_res = cultivation_money($p_info['id'],$ping_money,'体验店极差奖励',$order_info['id']);
        //没有找到平级的人这个钱不用扣除
        if($ping_res!==false){
            $money1 = bcsub($money1,$ping_money,2);
        }


        $repeat_money = bcmul($money1,0.05,2);
        $money1 = bcsub($money1,$repeat_money,2);
        //记录复购券
        repeatUserMoney($p_info['id'],$repeat_money,'11',$order_info['id']);


        $insert_data[] = [
            'u_id'=>$p_info['id'],
            'money'=>$money1,
            'order_id'=>$order_info['id'],
            'type'=>8,//门店津贴极差
            'symbol'=>1,
            'status'=>2,//直接到账
            'remark'=>'体验店店补极差奖励'.$money1,
            'created_at'=>date('Y-m-d H:i:s',time()),
            'updated_at'=>date('Y-m-d H:i:s',time())
        ];
    }



    $money2 = 0.00;
    if((int)$order_info['order_price']==9800 && $p_info['identity_id']==1){
        $money2 = 3330;
    }elseif ((int)$order_info['order_price']==9800 && $p_info['identity_id']==2){
        $money2 = 490;
    }elseif ((int)$order_info['order_price']==3980 && $p_info['identity_id']==1){
        $money2 = 1290;
    }elseif ((int)$order_info['order_price']==5820 && $p_info['identity_id']==1){
        $money2 = 2040;
    }elseif ((int)$order_info['order_price']==5820 && $p_info['identity_id']==2){
        $money2 = 2040;
    }

    //计算平级的钱
    $ping_money = bcmul($money2,0.1,2);

    //echo 123;
    //平级奖励
    $ping_res = cultivation_money($store_info['id'],$ping_money,'体验店极差奖励',$order_info['id']);
    //没有找到平级的人这个钱不用扣除
    if($ping_res!==false){
        $money2 = bcsub($money2,$ping_money,2);
    }


    $repeat_money = bcmul($money2,0.05,2);
    $money2 = bcsub($money2,$repeat_money,2);
    //记录复购券
    repeatUserMoney($store_info['id'],$repeat_money,'',$order_info['id']);

    //给店铺
    $insert_data[] = [
        'u_id'=>$store_info['id'],
        'money'=>$money2,
        'type'=>8,//门店津贴极差
        'symbol'=>1,
        'order_id'=>$order_info['id'],
        'status'=>2,//直接到账
        'remark'=>'认证店铺奖励'.$money2,
        'created_at'=>date('Y-m-d H:i:s',time()),
        'updated_at'=>date('Y-m-d H:i:s',time())
    ];
    if(!empty($store_up_parents)){
        foreach ($store_up_parents as $sku=>$skuv){
            if($skuv['identity_id']==4){
                if((int)$order_info['order_price']==5820){
                    $c_mane = 290;
                }else{
                    $c_mane = (int)$order_info['goods_price']==9800 ? 490: 200;
                }

                //计算平级的钱
                $ping_money = bcmul($c_mane,0.1,2);

                //echo 123;
                //平级奖励
                $ping_res = cultivation_money($skuv['id'],$ping_money,'体验店极差奖励',$order_info['id']);
                //没有找到平级的人这个钱不用扣除
                if($ping_res!==false){
                    $c_mane = bcsub($c_mane,$ping_money,2);
                }


                $repeat_money = bcmul($c_mane,0.05,2);
                $c_mane = bcsub($c_mane,$repeat_money,2);
                //记录复购券
                repeatUserMoney($skuv['id'],$repeat_money,'',$order_info['id']);

                //公司
                $insert_data[] = [
                    'u_id'=>$skuv['id'],
                    'money'=>$c_mane,
                    'type'=>8,//门店津贴极差
                    'symbol'=>1,
                    'order_id'=>$order_info['id'],
                    'status'=>2,//直接到账
                    'remark'=>'认证店铺奖励'.$c_mane,
                    'created_at'=>date('Y-m-d H:i:s',time()),
                    'updated_at'=>date('Y-m-d H:i:s',time())
                ];
            }elseif($skuv['identity_id']==5){
                \Illuminate\Support\Facades\Log::info('ssss');
                if(isset($store_up_parents[$sku-1])){
                    //给合伙人上面有公司
                    if((int)$order_info['order_price']==5820){
                        $t_mane = 290;
                    }else{
                        $t_mane = (int)$order_info['goods_price']==9800 ? 490: 200;
                    }

                    //计算平级的钱
                    $ping_money = bcmul($t_mane,0.1,2);

                    //echo 123;
                    //平级奖励
                    $ping_res = cultivation_money($skuv['id'],$ping_money,'体验店极差奖励',$order_info['id']);
                    //没有找到平级的人这个钱不用扣除
                    if($ping_res!==false){
                        $t_mane = bcsub($t_mane,$ping_money,2);
                    }


                    $repeat_money = bcmul($t_mane,0.05,2);
                    $t_mane = bcsub($t_mane,$repeat_money,2);
                    //记录复购券
                    repeatUserMoney($skuv['id'],$repeat_money,'',$order_info['id']);

                    //上面没有公司
                    $insert_data[] = [
                        'u_id'=>$skuv['id'],
                        'money'=>$t_mane,
                        'type'=>8,//门店津贴极差
                        'symbol'=>1,
                        'order_id'=>$order_info['id'],
                        'status'=>2,//直接到账
                        'remark'=>'认证店铺奖励'.$t_mane,
                        'created_at'=>date('Y-m-d H:i:s',time()),
                        'updated_at'=>date('Y-m-d H:i:s',time())
                    ];

                }else{
                    \Illuminate\Support\Facades\Log::info('ddddd');
                    $t_mane = (int)$order_info['goods_price']==9800 ? 980: 400;


                    //计算平级的钱
                    $ping_money = bcmul($t_mane,0.1,2);

                    //echo 123;
                    //平级奖励
                    $ping_res = cultivation_money($skuv['id'],$ping_money,'体验店极差奖励',$order_info['id']);
                    //没有找到平级的人这个钱不用扣除
                    if($ping_res!==false){
                        $t_mane = bcsub($t_mane,$ping_money,2);
                    }


                    $repeat_money = bcmul($t_mane,0.05,2);
                    $t_mane = bcsub($t_mane,$repeat_money,2);
                    //记录复购券
                    repeatUserMoney($skuv['id'],$repeat_money,'',$order_info['id']);


                    //上面没有公司
                    $insert_data[] = [
                        'u_id'=>$skuv['id'],
                        'money'=>$t_mane,
                        'type'=>8,//门店津贴极差
                        'symbol'=>1,
                        'order_id'=>$order_info['id'],
                        'status'=>2,//直接到账
                        'remark'=>'认证店铺奖励'.$t_mane,
                        'created_at'=>date('Y-m-d H:i:s',time()),
                        'updated_at'=>date('Y-m-d H:i:s',time())
                    ];
                }
            }
        }
    }


    \App\Models\TongUserProfit::query()->insert($insert_data);
    //修改用户身份
    $identity_id = (int)$order_info['goods_price']==9800?3:2;
    TongUser::query()->where('id',$user_info['id'])->update(['identity_id'=>$identity_id]);
    updateUserIdentity($user_info['id'],$identity_id);

    //$key = $store_info['identity_id'].'-'.$identity_id;
    //recommend_money_child_cha_tow($store_info['id'],$key,$order_id,$money1);

}

//function removeSmallerNeighbors($array, $key) {
//    if (count($array) < 2) {
//        return $array; // 数组不够两个元素，直接返回
//    }
//    \Illuminate\Support\Facades\Log::info('执行removeSmallerNeighbors');
//
//    for ($i = 1; $i <= count($array); $i++) {
//        if ($array[$i][$key] < $array[$i - 1][$key]) {
//            unset($array[$i]);
//            // 注意：这里移除元素后，数组的索引会发生变化，但for循环会继续递增$i
//            // 如果需要处理连续的比较，你可能需要调整循环逻辑
//        }
//    }
//
//    $array = array_values($array); // 重新索引数组
//    return $array;
//}
/**
 * 计算用户的百分比
 * @param $userInfo
 * @return array
 */
function userBaifenbi($userInfo){

    $data = [];
    if($userInfo['identity_id']==5){
        $data['desc']='当前身份是合伙人，已为最高身份';
        $data['baifenbi']=100;
    }
    if($userInfo['identity_id']==0){
        $data['desc']='当前身份是普通用户，暂无业务升级条件';
        $data['baifenbi']=0;
    }
    $user_list = getRedisUserData();
    //获取好友的直推人
    $zhi_user = getZhiUser($userInfo['id']);
    $zhi_user_ids = array_column($zhi_user,'id');
    $child_user = getChildLevel($user_list,$userInfo['id']);
    $child_user_ids = array_column($child_user,'id');
    if($userInfo['identity_id']==1) {
            //\Illuminate\Support\Facades\Log::info('');
            $zhi_num = TongUser::query()->whereIn('id', $zhi_user_ids)
                ->where('identity_id', '>=', 1)->count();
            $orderQuery = TongOrder::query()->whereIn('u_id', $child_user_ids);
            if (config('user.order_type') == 0) {
                $status_arr = [1, 3, 4, 5];
            } else {
                $status_arr = [5];
            }
            $order_num = $orderQuery->whereIn('status',$status_arr)->count();
            $key = $zhi_num.'-'.$order_num;
            if(!isset(config('user.upgrade_baifen.max_store')[$key])){
                $sheng_user = 3-$zhi_num;
                $sheng_order = 20-$order_num;
                //var_dump(config('user.upgrade_baifen.max_store')[$key]);
                $data['key'] = $key;
                $data['desc'] = '当前再分享'.$sheng_user.'位好友，销售'.$sheng_order.'单可升级为品牌会员';
                if(($zhi_num>3) && ($order_num==0) || ($zhi_num==0) && ($order_num>20)){
                    $data['baifenbi'] = 50;
                }

            }else{
                $sheng_user = 3-$zhi_num;
                $sheng_order = 20-$order_num;
                $data['desc'] = '当前再分享'.$sheng_user.'位好友，销售'.$sheng_order.'单可升级为品牌会员';
                $data['baifenbi'] = config('user.upgrade_baifen.max_store')[$key];
                $data['key'] = $key;
            }
    }
    if($userInfo['identity_id']==2) {
        //\Illuminate\Support\Facades\Log::info('');
        $zhi_num = TongUser::query()->whereIn('id', $zhi_user_ids)
            ->where('identity_id', '>=', 2)->count();
        $orderQuery = TongOrder::query()->whereIn('u_id', $child_user_ids);
        if (config('user.order_type') == 0) {
            $status_arr = [1, 3, 4, 5];
        } else {
            $status_arr = [5];
        }
        $order_num = $orderQuery->whereIn('status',$status_arr)->count();
        $key = $zhi_num.'-'.$order_num;
        if(!isset(config('user.upgrade_baifen.max_store')[$key])){
            $sheng_user = 3-$zhi_num;
            $sheng_order = 20-$order_num;
            //var_dump(config('user.upgrade_baifen.max_store')[$key]);
            $data['key'] = $key;
            $data['desc'] = '当前再分享'.$sheng_user.'位好友，销售'.$sheng_order.'单可升级为体验店';
            if(($zhi_num>3) && ($order_num==0) || ($zhi_num==0) && ($order_num>20)){
                $data['baifenbi'] = 50;
            }

        }else{
            $sheng_user = 3-$zhi_num;
            $sheng_order = 20-$order_num;
            $data['desc'] = '当前再分享'.$sheng_user.'位好友，销售'.$sheng_order.'单可升级为体验店';
            $data['baifenbi'] = config('user.upgrade_baifen.max_store')[$key];
            $data['key'] = $key;
        }
    }
    if($userInfo['identity_id']==3) {
        //\Illuminate\Support\Facades\Log::info('');
        $zhi_num = TongUser::query()->whereIn('id', $zhi_user_ids)
            ->where('identity_id', '>=', 3)->count();
        $orderQuery = TongOrder::query()->whereIn('u_id', $child_user_ids);
        if (config('user.order_type') == 0) {
            $status_arr = [1, 3, 4, 5];
        } else {
            $status_arr = [5];
        }
        $order_num = $orderQuery->whereIn('status',$status_arr)->count();
        $key = $zhi_num.'-'.$order_num;
        if(!isset(config('user.upgrade_baifen.max_store')[$key])){
            $sheng_user = 3-$zhi_num;
            $sheng_order = 20-$order_num;
            //var_dump(config('user.upgrade_baifen.max_store')[$key]);
            $data['key'] = $key;
            $data['desc'] = '当前再分享'.$sheng_user.'位好友，销售'.$sheng_order.'单可升级为公司';
            if(($zhi_num>3) && ($order_num==0) || ($zhi_num==0) && ($order_num>20)){
                $data['baifenbi'] = 50;
            }

        }else{
            $sheng_user = 3-$zhi_num;
            $sheng_order = 20-$order_num;
            $data['desc'] = '当前再分享'.$sheng_user.'位好友，销售'.$sheng_order.'单可升级为公司';
            $data['baifenbi'] = config('user.upgrade_baifen.max_store')[$key];
            $data['key'] = $key;
        }
    }
    if($userInfo['identity_id']==4) {
        //\Illuminate\Support\Facades\Log::info('');
        $zhi_num = TongUser::query()->whereIn('id', $zhi_user_ids)
            ->where('identity_id', '>=', 4)->count();
        $orderQuery = TongOrder::query()->whereIn('u_id', $child_user_ids);
        if (config('user.order_type') == 0) {
            $status_arr = [1, 3, 4, 5];
        } else {
            $status_arr = [5];
        }
        $order_num = $orderQuery->whereIn('status',$status_arr)->count();
        $key = $zhi_num.'-'.$order_num;
        if(!isset(config('user.upgrade_baifen.max_store')[$key])){
            $sheng_user = 3-$zhi_num;
            $sheng_order = 20-$order_num;
            //var_dump(config('user.upgrade_baifen.max_store')[$key]);
            $data['key'] = $key;
            $data['desc'] = '当前再分享'.$sheng_user.'位好友，销售'.$sheng_order.'单可升级为合伙人';
            if(($zhi_num>3) && ($order_num==0) || ($zhi_num==0) && ($order_num>20)){
                $data['baifenbi'] = 50;
            }

        }else{
            $sheng_user = 3-$zhi_num;
            $sheng_order = 20-$order_num;
            $data['desc'] = '当前再分享'.$sheng_user.'位好友，销售'.$sheng_order.'单可升级为合伙人';
            $data['baifenbi'] = config('user.upgrade_baifen.max_store')[$key];
            $data['key'] = $key;
        }
    }
    return $data;
}

/**
 * 公司统计
 * @return array|mixed
 * @throws \Psr\SimpleCache\InvalidArgumentException
 */
function getCompanyRes(){
    $user_list = getRedisUserData();
    //获取到身份位公司的人
    $user_list_res = array_filter($user_list,function($values){
        //var_dump($values);
        if($values['identity_id']==4){
            return $values;
        }
    });
    $user_list_res = array_values($user_list_res);
    if(count($user_list_res)==0){
        return [];
    }
    //dd($user_list_res);
    foreach ($user_list_res as $k=>&$v){
        $child_list = getChilds($user_list,$v['id']);
        $child_list_arr = array_filter($child_list,function($child_val){
            if(($child_val['identity_id']<4) && ($child_val['identity_id']>=1)){
                return $child_val;
            }
        });
        if(config('user.team.is_self')){
            // var_dump($v);
            array_push($child_list_arr,$v);
        }
        $v['child_user'] = $child_list_arr;
        $child_list_number = count($child_list_arr);//团队人数
        //echo '团队'.$k.'人数'.$child_list_number;
        $v['child_user'] = $child_list_arr;
    }
    return $user_list_res;
}


function getHehuoRes(){
    $user_list = getRedisUserData();
    //获取到身份位公司的人
    $user_list_res = array_filter($user_list,function($values){
        //var_dump($values);
        if($values['identity_id']==5){
            return $values;
        }
    });
    $user_list_res = array_values($user_list_res);
    if(count($user_list_res)==0){
        return [];
    }
    //dd($user_list_res);
    foreach ($user_list_res as $k=>&$v){
        $child_list = getChilds($user_list,$v['id']);
        $child_list_arr = array_filter($child_list,function($child_val){
            if(($child_val['identity_id']<5) && ($child_val['identity_id']>=1)){
                return $child_val;
            }
        });
        if(config('user.team.is_self')){
            // var_dump($v);
            array_push($child_list_arr,$v);
        }
        $v['child_user'] = $child_list_arr;
        $child_list_number = count($child_list_arr);//团队人数
        //echo '团队'.$k.'人数'.$child_list_number;
        $v['child_user'] = $child_list_arr;
    }
    return $user_list_res;
}

/**
 * @param $time
 * @return mixed
 */
function getUserHistory($time)
{
    $return_data = [];
    $time = (int)$time;
    $data = TongUser::where('created_at','<', Carbon::now())
        ->where('created_at','>', $time > 1 ? Carbon::today()->subDays($time):Carbon::today())
        ->select([$time > 1 ? DB::raw('DATE(created_at) as time') : DB::raw('DATE_FORMAT(created_at,\'%H\') as time'), DB::raw('COUNT("*") as count')])
        ->groupBy('time')
        ->get()->toArray();
    foreach ($data as $key=> $val){

        $return_data['time'][] = date('m-d',strtotime($val['time']));
            $return_data['count'][] = $val['count'];

    }
    return $return_data;
}

/**
 * 获取订单的折线图
 * @param $time
 * @return array
 */
function getOrderHistory($time)
{
    $return_data = [];
    $time = (int)$time;
    $data = TongOrder::where('created_at','<', Carbon::now())
        ->where('created_at','>', $time > 1 ? Carbon::today()->subDays($time):Carbon::today())
        ->whereIn('status',[1,3,4,5])
        ->where('is_test',0)
        ->select([$time > 1 ? DB::raw('DATE(created_at) as time') : DB::raw('DATE_FORMAT(created_at,\'%H\') as time'), DB::raw('COUNT("*") as count')])
        ->groupBy('time')
        ->get()->toArray();
    $zuijinDays = arrayDays($time);
//    foreach ($zuijinDays as $key=> $val){
//            if(isset($data[$key]['time'])){
//                array_search($data[$key]['time'],$zuijinDays);
//                $return_data['time'][] = date('m-d',strtotime($val));
//                $return_data['count'][] = $data[$key]['count'];
//            }else{
//                var_dump(456);
//                $return_data['time'][] = date('m-d',strtotime($val));
//                $return_data['count'][] = 0;
//            }
//
//    }
//    exit();

    foreach ($data as $key=> $val){

        $return_data['time'][] = date('m-d',strtotime($val['time']));
        $return_data['count'][] = $val['count'];

    }

    return $return_data;
}

/**
 * 获取最近几天日期
 * @return array
 */
function arrayDays($time){
    // 获取今天的日期
    $today = Carbon::now();

    // 获取近7天的日期数组
    $lastSevenDays = [];
    for ($i = 0; $i < $time; $i++) {
        // 将日期回退一天
        $date = $today->copy()->subDay();
        // 将日期格式化为Y-m-d
        $formattedDate = $date->format('Y-m-d');
        // 将格式化后的日期存入数组
        array_unshift($lastSevenDays, $formattedDate);
        // 继续回退一天
        $today->subDay();
    }
    return $lastSevenDays;
}

/**
 * 随机生成1毛至3毛的金额
 * @return float|int|void
 */
function randOrderMoney(){
    $min = 10; // 最小金额，单位为分
    $max = 30; // 最大金额，单位为分
    $randomAmount = mt_rand($min, $max) / 100; // 转换为元
    return $randomAmount;
}


/**
 * 另外一套的直推分配逻辑
 * @return false
 */
function new_user_recommend($user_id,$order_id){
    \Illuminate\Support\Facades\Log::info('走的直推间推逻辑');
    //获取用户的详情
    $user_info = TongUser::query()->where('id',$user_id)->first();
    if($user_info['p_id']==0){
        return false;
    }
    //获取邀请用户的身份信息
    $p_user_query = TongUser::query()->where('id',$user_info['p_id']);
    if(!$p_user_query->exists()){
        return false;
    }
    \Illuminate\Support\Facades\Log::info('走的直推间推逻辑1');
    $user_zhi_flow = [];
    $p_user_info = $p_user_query->first();
    //获取订单的信息
    $order_info = TongOrder::query()->where('id',$order_id)->first();
    //订单的总金额
    $all_order_price = $order_info['all_order_price'];
    \Illuminate\Support\Facades\Log::info('走的直推间推逻辑2');
    \Illuminate\Support\Facades\Log::info('邀请人的身份'.$p_user_info['identity_id']);
    if($p_user_info['identity_id']!=0){
        //获取用户的身分
        $p_identity_id = $p_user_info['identity_id'];
        \Illuminate\Support\Facades\Log::info('22222222222222');
        $u_key = $p_identity_id-1;
        $baifenbi = config('distribution.user')['v'.$u_key]['tuijian']['z'];
        \Illuminate\Support\Facades\Log::info('333333333');
        $baifenbi = bcdiv($baifenbi,100,2);//高精度除法
        \Illuminate\Support\Facades\Log::info('wwwww直推百分比'.$baifenbi);
        $money = bcmul($all_order_price,$baifenbi,2);
        if($money>0){

            $repeat_money = bcmul($money,0.05,2);
            $money = bcsub($money,$repeat_money,2);
            //记录复购券
            repeatUserMoney($p_user_info['id'],$repeat_money,'销售提成',$order_info['id'],10);

            $user_zhi_flow[] = [
                'u_id'=>$p_user_info['id'],
                'order_id'=>$order_id,
                'money'=>$money,
                'type'=>2,//推荐奖励
                'symbol'=>1,//类型收入
                'status'=>2, //状态待到账
                'remark'=>'销售提成奖励到账'.$money,
                'created_at'=>date('Y-m-d H:i:s',time()),
                'updated_at'=>date('Y-m-d H:i:s',time()),
            ];
            if(config("test.test_start")==1 && in_array($user_id,explode(',',str_replace('，', ',', config("test.test_user"))))){
                \App\Models\TongUserProfitTest::query()->insert($user_zhi_flow);
            }else{
                \App\Models\TongUserProfit::query()->insert($user_zhi_flow);
            }

        }
    }
    \Illuminate\Support\Facades\Log::info('走的直推间推逻辑3');
    $user_jian_flow = [];
    //获取间推用户的身份信息
    $pp_user_query = TongUser::query()->where('id',$p_user_info['p_id']);
    if(!$pp_user_query->exists()){
        return false;
    }
    $pp_user_info = $pp_user_query->first();
    \Illuminate\Support\Facades\Log::info('走的直推间推逻辑4');
    if($pp_user_info['identity_id']!=0){
        \Illuminate\Support\Facades\Log::info('走的直推间推逻辑5');
        //获取用户的身分
        $pp_identity_id = $pp_user_info['identity_id'];
        $u_key = $pp_identity_id-1;
        $baifenbi = config('distribution.user')['v'.$u_key]['tuijian']['j'];
        $baifenbi = bcdiv($baifenbi,100,2);//高精度除法
        $money = bcmul($all_order_price,$baifenbi,2);
        if($money>0){
            $repeat_money = bcmul($money,0.05,2);
            $money = bcsub($money,$repeat_money,2);
            //记录复购券
            repeatUserMoney($pp_user_info['id'],$repeat_money,'服务提成',$order_info['id'],10);

            $user_jian_flow[] = [
                'u_id'=>$pp_user_info['id'],
                'order_id'=>$order_id,
                'money'=>$money,
                'type'=>2,//推荐奖励
                'symbol'=>1,//类型收入
                'status'=>2, //状态待到账
                'remark'=>'服务提成奖励到账'.$money,
                'created_at'=>date('Y-m-d H:i:s',time()),
                'updated_at'=>date('Y-m-d H:i:s',time()),
            ];
            \Illuminate\Support\Facades\Log::info('走的直推间推逻辑6');
            if(config("test.test_start")==1 && in_array($user_id,explode(',',str_replace('，', ',', config("test.test_user"))))){
                \App\Models\TongUserProfitTest::query()->insert($user_jian_flow);
            }else{
                \App\Models\TongUserProfit::query()->insert($user_jian_flow);
            }
        }
    }

}

/**
 * 用户复购
 * @return false
 */
function new_user_fugou($user_id,$order_id){
    \Illuminate\Support\Facades\Log::info('执行复购');
    //获取用户的详情
    $user_info = TongUser::query()->where('id',$user_id)->first();
    if($user_info['p_id']==0){
        return false;
    }
    //获取邀请用户的身份信息
    $p_user_query = TongUser::query()->where('id',$user_info['p_id']);
    if(!$p_user_query->exists()){
        return false;
    }
    $user_zhi_fu_flow = [];
    $p_user_info = $p_user_query->first();
    //获取订单的信息
    $order_info = TongOrder::query()->where('id',$order_id)->first();
    //订单的总金额
    $all_order_price = $order_info['all_order_price'];
    \Illuminate\Support\Facades\Log::info('执行复购身份'.$p_user_info['identity_id']);
    if($p_user_info['identity_id']!=0){
        //获取用户的身分
        $p_identity_id = $p_user_info['identity_id'];
        $u_key = $p_identity_id-1;
        $baifenbi = config('distribution.user')['v'.$u_key]['fugou']['z'];
        if($order_info['goods_id']==14){
            $baifenbi = $baifenbi+5;
        }
        $baifenbi = bcdiv($baifenbi,100,2);//高精度除法
        \Illuminate\Support\Facades\Log::info('直推身份'.config('user.identity')[$p_identity_id].'收益百分比'.$baifenbi);
        $money = bcmul($all_order_price,$baifenbi,2);
        \Illuminate\Support\Facades\Log::info('执行复购获得金额'.$money);
        if($money>0){

            $repeat_money = bcmul($money,0.05,2);
            $money = bcsub($money,$repeat_money,2);
            //记录复购券
            repeatUserMoney($p_user_info['id'],$repeat_money,'销售补贴',$order_info['id'],10);

            $user_zhi_fu_flow[] = [
                'u_id'=>$p_user_info['id'],
                'order_id'=>$order_id,
                'money'=>$money,
                'type'=>2,//推荐奖励
                'symbol'=>1,//类型收入
                'status'=>2, //状态待到账
                'remark'=>'销售补贴奖励到账'.$money,
                'created_at'=>date('Y-m-d H:i:s',time()),
                'updated_at'=>date('Y-m-d H:i:s',time()),
            ];
            if(config("test.test_start")==1 && in_array($user_id,explode(',',str_replace('，', ',', config("test.test_user"))))){
                \App\Models\TongUserProfitTest::query()->insert($user_zhi_fu_flow);
            }else{
                \App\Models\TongUserProfit::query()->insert($user_zhi_fu_flow);
            }

        }

    }
    $user_jian_fu_flow = [];
    //获取间推用户的身份信息
    $pp_user_query = TongUser::query()->where('id',$p_user_info['p_id']);
    if(!$pp_user_query->exists()){
        return false;
    }
    $pp_user_info = $pp_user_query->first();

    if($pp_user_info['identity_id']!=0){
        //获取用户的身分
        $pp_identity_id = $pp_user_info['identity_id'];
        $u_key = $pp_identity_id-1;
        $baifenbi = config('distribution.user')['v'.$u_key]['fugou']['j'];
        $baifenbi = bcdiv($baifenbi,100,2);//高精度除法
        \Illuminate\Support\Facades\Log::info('间推身份'.config('user.identity')[$pp_identity_id].'收益百分比'.$baifenbi);
        $money = bcmul($all_order_price,$baifenbi,2);
        if($money>0){

            $repeat_money = bcmul($money,0.05,2);
            $money = bcsub($money,$repeat_money,2);
            //记录复购券
            repeatUserMoney($pp_user_info['id'],$repeat_money,'服务补贴',$order_info['id'],10);

            $user_jian_fu_flow[] = [
                'u_id'=>$pp_user_info['id'],
                'order_id'=>$order_id,
                'money'=>$money,
                'type'=>2,//推荐奖励
                'symbol'=>1,//类型收入
                'status'=>2, //状态待到账
                'remark'=>'服务补贴奖励到账'.$money,
                'created_at'=>date('Y-m-d H:i:s',time()),
                'updated_at'=>date('Y-m-d H:i:s',time()),
            ];
            if(config("test.test_start")==1 && in_array($user_id,explode(',',str_replace('，', ',', config("test.test_user"))))){
                \App\Models\TongUserProfitTest::query()->insert($user_jian_fu_flow);
            }else{
                \App\Models\TongUserProfit::query()->insert($user_jian_fu_flow);
            }
        }
    }
}

/**
 * @param $user_id
 * @param $order_id
 * @return false|void
 */
function new_store_bu($user_id,$order_id){
    \Illuminate\Support\Facades\Log::info('执行了店补');
    //获取用户的详情
    $user_list = getRedisUserData();
    $all_parents = getAllParents($user_list, $user_id);
    //找出用户身份比自己等级高的
    if(empty($all_parents)){
        \Illuminate\Support\Facades\Log::info('直推奖励差集没有找到父级');
        return false;
    }

    $temp_arr_parents = [];
    //数组去除重复
    foreach ($all_parents as $rv){
        if(!array_key_exists($rv['id'],$temp_arr_parents)){
            $temp_arr_parents[$rv['id']] = $rv;
        }
    }
    $all_parents = array_values($temp_arr_parents);

    unset($all_parents[0]);
    $all_parents = array_values($all_parents);
    //过滤出来是指定店铺的人
    //找出指定的店铺
    $all_parents = array_filter($all_parents,function($parents_val){
        return $parents_val['identity_id']==3 or $parents_val['identity_id']==4;
    });
    $all_parents = array_values($all_parents);
    \Illuminate\Support\Facades\Log::info('执行查找指定商店',$all_parents);
    if(!$all_parents){
        //没找到指定店铺
        return false;
    }

    $p_user_info = TongUser::query()->where('id',$all_parents[0]['id'])->first();
    //获取订单的信息
    $order_info = TongOrder::query()->where('id',$order_id)->first();
    //订单的总金额
    $all_order_price = $order_info['all_order_price'];
    $user_store_flow = [];
    if($p_user_info['identity_id']!=0){
        $p_identity_id = $p_user_info['identity_id'];
        $u_key = $p_identity_id-1;
        \Illuminate\Support\Facades\Log::info('店补hhhhh'.config('distribution.user')['v'.$u_key]['store']['z']);

            $baifenbi = config('distribution.user')['v'.$u_key]['store']['z'];
            $baifenbi = bcdiv($baifenbi,100,2);//高精度除法
            $money = bcmul($all_order_price,$baifenbi,2);
            \Illuminate\Support\Facades\Log::info('店补百分比'.$money);
            if($money>0){

                $repeat_money = bcmul($money,0.05,2);
                $money = bcsub($money,$repeat_money,2);
                //记录复购券
                repeatUserMoney($p_user_info['id'],$repeat_money,'新店扶持',$order_info['id'],10);

                $user_store_flow[] = [
                    'u_id'=>$p_user_info['id'],
                    'order_id'=>$order_id,
                    'money'=>$money,
                    'type'=>3,//推荐奖励
                    'symbol'=>1,//类型收入
                    'status'=>2, //状态待到账
                    'remark'=>'新店扶持奖励到账'.$money,
                    'created_at'=>date('Y-m-d H:i:s',time()),
                    'updated_at'=>date('Y-m-d H:i:s',time()),
                ];
                \App\Models\TongUserProfit::query()->insert($user_store_flow);

            }


    }
}

/**
 * 复制出来了一份区域奖励用来补发复购的区域奖励
 * @param $user_id
 * @param $order_id
 * @return false|void
 * @throws DaoException
 */
function new_area_money_copy($user_id,$order_id){
    \Illuminate\Support\Facades\Log::info('执行区域津贴');
    //获取用户的信息
    $user_info = TongUser::query()->where('id',$user_id)->first();
    //获取订单的详情->where('status',1)
    $order_query = TongOrder::query()->where('u_id',$user_id)->where('id',$order_id)->whereIn('status',[1,4]);
    \Illuminate\Support\Facades\Log::info('执行区域津贴1');
    if(!$order_query->exists()){
        return false;
    }
    \Illuminate\Support\Facades\Log::info('执行区域津贴2');
    $order_info = $order_query->first();
    $all_order_price = $order_info['all_order_price'];

    //查询订单的地址信息
    $order_address_info = $order_info['order_address_info'];

    //获取订单省市区的人

    \Illuminate\Support\Facades\Log::info('订单地址信息',[$order_address_info['city_id'],$order_address_info['area_id']]);

    $city_user_query = \App\Models\TongUserAreaLog::query()->where('city_id',$order_address_info['city_id'])->where('identity_id',6);
    $area_user_query = App\Models\TongUserAreaLog::query()->where('area_id',$order_address_info['area_id'])->where('identity_id',5);


    if($city_user_query->exists()){
        \Illuminate\Support\Facades\Log::info('找到了城市');
        $city_user_info = $city_user_query->first();
        $city_user_info_identity_id = $city_user_info['identity_id'];
        $u_key = $city_user_info_identity_id-1;

        $baifenbi = config('distribution.user')['v'.$u_key]['area']['z'];
        $baifenbi = bcdiv($baifenbi,100,2);//高精度除法
        $money = bcmul($all_order_price,$baifenbi,2);
        \Illuminate\Support\Facades\Log::info('市百分比'.$baifenbi);
        \Illuminate\Support\Facades\Log::info('市金额'.$money);
        if($money>0){
            $repeat_money = bcmul($money,0.05,2);
            $money = bcsub($money,$repeat_money,2);
            //记录复购券
            repeatUserMoney($city_user_info['u_id'],$repeat_money,'市代津贴',$order_info['id'],10);
            $temp_data = [
                'u_id'=>$city_user_info['u_id'],
                'order_id'=>$order_id,
                'money'=>$money,
                'type'=>4,//推荐奖励
                'symbol'=>1,//类型收入
                'status'=>2, //状态待到账
                'remark'=>'市代津贴奖励'.$money,
            ];
            if(config("test.test_start")==1 && in_array($user_id,explode(',',str_replace('，', ',', config("test.test_user"))))){
                \App\Models\TongUserProfitTest::query()->create($temp_data);
            }else{
                \App\Models\TongUserProfit::query()->create($temp_data);
            }

        }
    }

    //123
    if($area_user_query->exists()){
        \Illuminate\Support\Facades\Log::info('找到了区域');
        $area_user_info = $area_user_query->first();
        $area_user_info_identity_id = $area_user_info['identity_id'];
        $u_key = $area_user_info_identity_id-1;

        $baifenbi = config('distribution.user')['v'.$u_key]['area']['z'];
        $baifenbi = bcdiv($baifenbi,100,2);//高精度除法
        $money = bcmul($all_order_price,$baifenbi,2);
        \Illuminate\Support\Facades\Log::info('区县百分比'.$baifenbi);
        \Illuminate\Support\Facades\Log::info('区县金额'.$money);
        if($money>0){
            $repeat_money = bcmul($money,0.05,2);
            $money = bcsub($money,$repeat_money,2);
            //记录复购券
            repeatUserMoney($area_user_info['u_id'],$repeat_money,'区县代津贴',$order_info['id'],10);

            $temp_data = [
                'u_id'=>$area_user_info['u_id'],
                'order_id'=>$order_id,
                'money'=>$money,
                'type'=>4,//推荐奖励
                'symbol'=>1,//类型收入
                'status'=>2, //状态待到账
                'remark'=>'区县代津贴奖励'.$money,
            ];
            if(config("test.test_start")==1 && in_array($user_id,explode(',',str_replace('，', ',', config("test.test_user"))))){
                \App\Models\TongUserProfitTest::query()->create($temp_data);
            }else{
                \App\Models\TongUserProfit::query()->create($temp_data);
            }

        }
    }



}

function new_area_money($user_id,$order_id){
    \Illuminate\Support\Facades\Log::info('执行区域津贴');
    //获取用户的信息
    $user_info = TongUser::query()->where('id',$user_id)->first();
    //获取订单的详情->where('status',1)
    $order_query = TongOrder::query()->where('u_id',$user_id)->where('id',$order_id)->where('status',1);
    \Illuminate\Support\Facades\Log::info('执行区域津贴1');
    if(!$order_query->exists()){
        return false;
    }
    \Illuminate\Support\Facades\Log::info('执行区域津贴2');
    $order_info = $order_query->first();
    $all_order_price = $order_info['all_order_price'];

    //查询订单的地址信息
    $order_address_info = $order_info['order_address_info'];

    //获取订单省市区的人

    \Illuminate\Support\Facades\Log::info('订单地址信息',[$order_address_info['city_id'],$order_address_info['area_id']]);

    $city_user_query = \App\Models\TongUserAreaLog::query()->where('city_id',$order_address_info['city_id'])->where('identity_id',6);
    $area_user_query = App\Models\TongUserAreaLog::query()->where('area_id',$order_address_info['area_id'])->where('identity_id',5);


    if($city_user_query->exists()){
        \Illuminate\Support\Facades\Log::info('找到了城市');
        $city_user_info = $city_user_query->first();
        $city_user_info_identity_id = $city_user_info['identity_id'];
        $u_key = $city_user_info_identity_id-1;

        $baifenbi = config('distribution.user')['v'.$u_key]['area']['z'];
        $baifenbi = bcdiv($baifenbi,100,2);//高精度除法
        $money = bcmul($all_order_price,$baifenbi,2);
        \Illuminate\Support\Facades\Log::info('市百分比'.$baifenbi);
        \Illuminate\Support\Facades\Log::info('市金额'.$money);
        if($money>0){
            $repeat_money = bcmul($money,0.05,2);
            $money = bcsub($money,$repeat_money,2);
            //记录复购券
            repeatUserMoney($city_user_info['u_id'],$repeat_money,'市代津贴',$order_info['id'],10);
            $temp_data = [
                'u_id'=>$city_user_info['u_id'],
                'order_id'=>$order_id,
                'money'=>$money,
                'type'=>4,//推荐奖励
                'symbol'=>1,//类型收入
                'status'=>2, //状态待到账
                'remark'=>'市代津贴奖励'.$money,
            ];
            if(config("test.test_start")==1 && in_array($user_id,explode(',',str_replace('，', ',', config("test.test_user"))))){
                \App\Models\TongUserProfitTest::query()->create($temp_data);
            }else{
                \App\Models\TongUserProfit::query()->create($temp_data);
            }

        }
    }

    //123
    if($area_user_query->exists()){
        \Illuminate\Support\Facades\Log::info('找到了区域');
        $area_user_info = $area_user_query->first();
        $area_user_info_identity_id = $area_user_info['identity_id'];
        $u_key = $area_user_info_identity_id-1;

        $baifenbi = config('distribution.user')['v'.$u_key]['area']['z'];
        $baifenbi = bcdiv($baifenbi,100,2);//高精度除法
        $money = bcmul($all_order_price,$baifenbi,2);
        \Illuminate\Support\Facades\Log::info('区县百分比'.$baifenbi);
        \Illuminate\Support\Facades\Log::info('区县金额'.$money);
        if($money>0){
            $repeat_money = bcmul($money,0.05,2);
            $money = bcsub($money,$repeat_money,2);
            //记录复购券
            repeatUserMoney($area_user_info['u_id'],$repeat_money,'区县代津贴',$order_info['id'],10);

            $temp_data = [
                'u_id'=>$area_user_info['u_id'],
                'order_id'=>$order_id,
                'money'=>$money,
                'type'=>4,//推荐奖励
                'symbol'=>1,//类型收入
                'status'=>2, //状态待到账
                'remark'=>'区县代津贴奖励'.$money,
            ];
            if(config("test.test_start")==1 && in_array($user_id,explode(',',str_replace('，', ',', config("test.test_user"))))){
                \App\Models\TongUserProfitTest::query()->create($temp_data);
            }else{
                \App\Models\TongUserProfit::query()->create($temp_data);
            }

        }
    }



}

/**
 * 返回用户的直推间推id
 * @param $user_id
 * @return mixed
 */
function getUserZAndJ($user_id){
    $j = [];
    $z = TongUser::query()->where('p_id',$user_id)->pluck('id')->toArray();
    if(!empty($z)){
        $j = TongUser::query()->whereIn('p_id',$z)->pluck('id')->toArray();
    }
    return array_merge($z,$j);
}

/**
 * 统计伞下成员业绩
 * @param $user_id
 * @return int|mixed
 */
function get_child_order_num($user_id){
    //
    $user_info = TongUser::query()->where('id',$user_id)->first();
    if(empty($user_info['child_id'])) return 0;
    $child_arr = explode(',',$user_info['child_id']);
    $firstDayOfMonth = date('Y-m-01 00:00:00');
    // 本月结束时间
    $lastDayOfMonth = date('Y-m-t 23:59:59');
    return TongOrder::query()->whereIn('status',[
        1,3,4,5
    ])->whereIn('u_id',$child_arr)->whereBetween('created_at',[$firstDayOfMonth,$lastDayOfMonth])->sum('all_order_price');
}

/**
 * 用户的订单统计
 * @param $user_id
 * @return void
 */
function user_statistics_order($user_id,$money){

    while($user_id!=0)
    {
        TongUser::query()->where('id',$user_id)->increment('child_month_order',$money);
        $user_info =TongUser::query()->where('id',$user_id)->first();
        $user_id = $user_info['p_id'];
    }
}

/**
 * 向上查人
 * @param $user_id
 * @return array
 */
function get_line_user($user_id){
    $result_arr = [];
    while($user_id!=0)
    {
        $user_info =TongUser::query()->where('id',$user_id)->first();
        $result_arr[] = [
            'id'=>$user_info['p_id'],
            'nickname'=>$user_info['nickname'],
            'identity_id'=>$user_info['identity_id'],
        ];
        $user_id = $user_info['p_id'];
    }
    return $result_arr;
}

/**
 * 查找领导用户
 * @param $user_id
 * @return void
 */
function find_leader_user($user_id){
    $find_id = 0;
    while($user_id!=0)
    {
        $user_info =TongUser::query()->where('id',$user_id)->first();
        if($user_info['is_leader']==1){
            $user_id = 0;
            $find_id = $user_info['id'];
        }else{
            $user_id = $user_info['p_id'];
        }

    }
    return $find_id;
}


/**
 * 根据用户信息获取百分比
 * @param $item
 * @param $child_month_order
 * @return int
 */
function get_user_baifenbi($item,$child_month_order){
    $baifenbi = 0;
    if($item['identity_id']==2){
        if($child_month_order>=10000 && $child_month_order<=100000){
            $baifenbi = 2;
        }elseif ($child_month_order>100000 && $child_month_order<=200000){
            $baifenbi = 3;
        }elseif ($child_month_order>200000 && $child_month_order<=300000){
            $baifenbi = 4;
        }elseif ($child_month_order>300000 && $child_month_order<=500000){
            $baifenbi = 5;
        }elseif ($child_month_order>500000 && $child_month_order<=800000){
            $baifenbi = 6;
        }elseif ($child_month_order>800000 && $child_month_order<=1200000){
            $baifenbi = 7;
        }elseif ($child_month_order>1200000 && $child_month_order<=2000000){
            $baifenbi = 8;
        }elseif($child_month_order>2000000){
            $baifenbi = 8;
        }
    }elseif($item['identity_id']>=3){
        if($child_month_order>=10000 && $child_month_order<=100000){
            $baifenbi = 2;
        }elseif ($child_month_order>100000 && $child_month_order<=200000){
            $baifenbi = 3;
        }elseif ($child_month_order>200000 && $child_month_order<=300000){
            $baifenbi = 4;
        }elseif ($child_month_order>300000 && $child_month_order<=500000){
            $baifenbi = 5;
        }elseif ($child_month_order>500000 && $child_month_order<=800000){
            $baifenbi = 6;
        }elseif ($child_month_order>800000 && $child_month_order<=1200000){
            $baifenbi = 7;
        }elseif ($child_month_order>1200000 && $child_month_order<=2000000){
            $baifenbi = 8;
        }elseif($child_month_order>2000000 && $child_month_order<=3000000){
            $baifenbi = 9;
        }elseif($child_month_order>3000000){
            $baifenbi = 10;
        }
    }
    return $baifenbi;
}

/**
 * @param $item
 * @param $child_month_order
 * @return int
 */
function get_user_baifenbi_tow($child_month_order){
    $baifenbi = 0;
    if($child_month_order>=10000 && $child_month_order<=100000){
        $baifenbi = 2;
    }elseif ($child_month_order>100000 && $child_month_order<=200000){
        $baifenbi = 3;
    }elseif ($child_month_order>200000 && $child_month_order<=300000){
        $baifenbi = 4;
    }elseif ($child_month_order>300000 && $child_month_order<=500000){
        $baifenbi = 5;
    }elseif ($child_month_order>500000 && $child_month_order<=800000){
        $baifenbi = 6;
    }elseif ($child_month_order>800000 && $child_month_order<=1200000){
        $baifenbi = 7;
    }elseif ($child_month_order>1200000 && $child_month_order<=2000000){
        $baifenbi = 8;
    }elseif($child_month_order>2000000 && $child_month_order<=3000000){
        $baifenbi = 9;
    }elseif($child_month_order>3000000){
        $baifenbi = 10;
    }
    return $baifenbi;
}


/**
 * 按月计算每个人的团队收益
 * @return void
 */
function statistics_month_money()
{
    //echo 123;
    $query = \App\Models\TongUserMonthMoney::query()->where('status',1)
        ->where('child_month_order','>',10000);

    if(!$query->exists()){
        return false;
    }



    $users = $query->get()->toArray();



    $user_flow_data = [];
    foreach ($users as $item){
        $user_id = $item['u_id'];
        //用户当前的业绩
        $child_month_order=$item['child_month_order'];

        //$baifenbi = get_user_baifenbi($item,$child_month_order);
        $baifenbi = get_user_baifenbi_tow($child_month_order);
        //echo $user_id.'百分比'.$baifenbi.'%'.'<br/>';


        //获取用户的直推
        $zhi_user_query = \App\Models\TongUserMonthMoney::query()->where('p_id',$user_id)->where('status',1);
        $child_all_money = 0; //统计直推用户的业绩
        if($zhi_user_query->exists()){
            $zhi_user_arr = $zhi_user_query->get()->toArray();

            $child_all_money = 0;//存储每个直推用户的钱
            foreach ($zhi_user_arr as $ck=>$cv){
                //$z_user_id = $cv['id'];
                //用户当前的业绩
                $z_month_order=$cv['child_month_order'];
                $child_all_money = bcadd($child_all_money,$z_month_order,2);

                //$z_baifenbi = get_user_baifenbi($cv,$z_month_order);
                $z_baifenbi = get_user_baifenbi_tow($z_month_order);
                //echo '直推用户业绩'.$z_month_order.'百分比'.$z_baifenbi.'<br/>';
                $cha_baifenbi = $baifenbi-$z_baifenbi;
                //$child_all_money 是统计出来下级用户给钱的总额 （先注释掉）
                //$child_all_money=bcadd($child_all_money,bcmul($z_month_order,bcdiv($cha_baifenbi,100,2),2),2);
                $temp_money = bcmul($z_month_order,bcdiv($cha_baifenbi,100,2),2);
                $temp_user_info = TongUser::query()->where('id',$cv['u_id'])->first();
                if($temp_money>0 && $item['identity_id']>=2){
                    $user_flow_data[] = [
                        'u_id'=>$user_id,
                        'team_id'=>$user_id,
                        'money'=>$temp_money,
                        'type'=>5,//计算团队业绩
                        'symbol'=>1,
                        'status'=>2,
                        'remark'=>$temp_user_info['nickname'].'用户'.'管理津贴奖励+'.$temp_money,
                        'created_at'=>date('Y-m-d H:i:s',time()),
                        'updated_at'=>date('Y-m-d H:i:s',time())
                    ];
                }
            }
            $self_month_order = bcsub($child_month_order,$child_all_money,2);
            if($self_month_order>0){
                $temp_self_info = TongUser::query()->where('id',$user_id)->first();
                $self_money =bcmul($self_month_order,bcdiv($baifenbi,100,2));
                if($self_money>0){
                    $user_flow_data[] = [
                        'u_id'=>$user_id,
                        'team_id'=>$user_id,
                        'money'=>$self_money,
                        'type'=>5,//计算团队业绩
                        'symbol'=>1,
                        'status'=>2,
                        'remark'=>$temp_self_info['nickname'].'用户'.'管理津贴奖励+'.$self_money,
                        'created_at'=>date('Y-m-d H:i:s',time()),
                        'updated_at'=>date('Y-m-d H:i:s',time())
                    ];
                }
            }
            //这里是吧下级给的钱统计成为一条
            /*            \Illuminate\Support\Facades\Log::info('=====>业绩',[
                            'money'=>$child_all_money,
                            'child_month_order'=>$child_month_order,
                            'user_id'=>$user_id,
                            'identity_id'=>$item['identity_id']
                        ]);

                        if($child_all_money>0 && $item['identity_id']>=2){
                            $user_flow_data[] = [
                                'u_id'=>$user_id,
                                'team_id'=>$user_id,
                                'money'=>$child_all_money,
                                'type'=>5,//计算团队业绩
                                'symbol'=>1,
                                'status'=>2,
                                'remark'=>'管理津贴奖励+'.$child_all_money,
                                'created_at'=>date('Y-m-d H:i:s',time()),
                                'updated_at'=>date('Y-m-d H:i:s',time())
                            ];
                        }*/
        }
        //dd($user_flow_data);
        //var_dump($user_flow_data);

    }
    \Illuminate\Support\Facades\Log::info('业绩数组',$user_flow_data);
    DB::beginTransaction();
    try {
        if(!empty($user_flow_data)){
            \App\Models\TongUserProfit::query()->insert($user_flow_data);
        }
        \App\Models\TongUserMonthMoney::query()->update(['status'=>2]);
        DB::commit();
    }catch (Exception $exception){
        DB::rollBack();
    }
}


/**
 * 导出excel
 * @return false|void
 */
function statistics_month_money_excel()
{
    //echo 123;
    $query = \App\Models\TongUserMonthMoney::query()->where('status',1)
        ->where('child_month_order','>',10000);

    if(!$query->exists()){
        return false;
    }



    $users = $query->get()->toArray();



    $user_flow_data = [];
    foreach ($users as $item){
        $user_id = $item['u_id'];
        //用户当前的业绩
        $child_month_order=$item['child_month_order'];

        //$baifenbi = get_user_baifenbi($item,$child_month_order);
        $baifenbi = get_user_baifenbi_tow($child_month_order);
        //echo $user_id.'百分比'.$baifenbi.'%'.'<br/>';


        //获取用户的直推
        $zhi_user_query = \App\Models\TongUserMonthMoney::query()->where('p_id',$user_id)->where('status',1);
        $child_all_money = 0; //统计直推用户的业绩
        if($zhi_user_query->exists()){
            $zhi_user_arr = $zhi_user_query->get()->toArray();

            $child_all_money = 0;//存储每个直推用户的钱
            foreach ($zhi_user_arr as $ck=>$cv){
                //$z_user_id = $cv['id'];
                //用户当前的业绩
                $z_month_order=$cv['child_month_order'];
                $child_all_money = bcadd($child_all_money,$z_month_order,2);

                //$z_baifenbi = get_user_baifenbi($cv,$z_month_order);
                $z_baifenbi = get_user_baifenbi_tow($z_month_order);
                //echo '直推用户业绩'.$z_month_order.'百分比'.$z_baifenbi.'<br/>';
                $cha_baifenbi = $baifenbi-$z_baifenbi;
                //$child_all_money 是统计出来下级用户给钱的总额 （先注释掉）
                //$child_all_money=bcadd($child_all_money,bcmul($z_month_order,bcdiv($cha_baifenbi,100,2),2),2);
                $temp_money = bcmul($z_month_order,bcdiv($cha_baifenbi,100,2),2);
                $temp_user_info = TongUser::query()->where('id',$cv['u_id'])->first();
                $userItemInfo = TongUser::query()->where('id',$user_id)->first();
                if($temp_money>0 && $item['identity_id']>=2){
                    $user_flow_data[] = [
                        'share_id'=>$user_id,
                        'user_id'=>$temp_user_info['id'],
                        'user_nickname'=>$temp_user_info['nickname'],
                        'share_nickname'=>$userItemInfo['nickname'],//计算团队业绩
                        'yeji'=>$cv['child_month_order'],
                        'time_create'=>$cv['created_at'],
                        'share_yeji'=>$item['child_month_order'],
                        'tuijianren_baifenbi'=>$baifenbi,
                        'me_baifen'=>$z_baifenbi,
                        'chazhi'=>$cha_baifenbi,
                        'yongjin'=>$temp_money,
                        'title'=>$temp_user_info['nickname'].'用户'.'管理津贴奖励+'.$temp_money,
                    ];
                }else{
                    $user_flow_data[] = [
                        'share_id'=>$user_id,
                        'user_id'=>$temp_user_info['id'],
                        'user_nickname'=>'',
                        'share_nickname'=>'',//计算团队业绩
                        'yeji'=>0,
                        'time_create'=>$cv['created_at'],
                        'share_yeji'=>'',
                        'tuijianren_baifenbi'=>'',
                        'me_baifen'=>'',
                        'chazhi'=>'',
                        'yongjin'=>'',
                        'title'=>'',
                    ];
                }
            }
            $self_month_order = bcsub($child_month_order,$child_all_money,2);
            if($self_month_order>0){
                $temp_self_info = TongUser::query()->where('id',$user_id)->first();
                $self_money =bcmul($self_month_order,bcdiv($baifenbi,100,2));
                if($self_money>0){
                    $user_flow_data[] = [
                        'share_id'=>$user_id,
                        'user_id'=>$userItemInfo['id'],
                        'user_nickname'=>$userItemInfo['nickname'],
                        'share_nickname'=>$userItemInfo['nickname'],//计算团队业绩
                        'yeji'=>$self_month_order,
                        'time_create'=>$cv['created_at'],
                        'share_yeji'=>$item['child_month_order'],
                        'tuijianren_baifenbi'=>$baifenbi,
                        'me_baifen'=>$baifenbi,
                        'chazhi'=>0,
                        'yongjin'=>$self_money,
                        'title'=>$temp_self_info['nickname'].'用户'.'管理津贴奖励+'.$self_money,
                    ];
                }else{
                    $user_flow_data[] = [
                        'share_id'=>$user_id,
                        'user_id'=>$userItemInfo['id'],
                        'user_nickname'=>'',
                        'share_nickname'=>'',//计算团队业绩
                        'yeji'=>0,
                        'time_create'=>$cv['created_at'],
                        'share_yeji'=>'',
                        'tuijianren_baifenbi'=>'',
                        'me_baifen'=>'',
                        'chazhi'=>'',
                        'yongjin'=>'',
                        'title'=>'',
                    ];
                }
            }
        }
        //var_dump($user_flow_data);

    }

    DB::table('tong_user_test')->insert($user_flow_data);

}


function updateUserPid($user_id,$p_id){
    $user_info = RedisService::hGet($user_id);
    $user_info['p_id'] = $p_id;
    RedisService::hUpdate($user_id,$user_info);
}

/**
 * @param $user_id
 * @param $order_id
 * @return void
 */
function store_money_two($user_id,$order_id)
{
    //获取用户据信息
    $user_info = TongUser::query()->where('id',$user_id)->first();
    //获取订单信息
    $order_info = TongOrder::query()->where('id',$order_id)->first();

    //$money = ($user_info['identity_id']==1) ? config('user.store_buzu.tiyan'):config('user.store_buzu.pinpai');

//    if ((int)$order_info['order_price']==3980 || (int)$order_info['order_price']==3581){
//        $money = $money-40;
//    }
    //获取订单的信息
    //$order_info = TongOrder::query()->where('id',$order_id)->first();
    //订单的总金额
    $all_order_price = $order_info['all_order_price'];


    $baifenbi = 10;
    $baifenbi = bcdiv($baifenbi,100,2);//高精度除法
    $money = bcmul($all_order_price,$baifenbi,2);
    \Illuminate\Support\Facades\Log::info('店补百分比'.$money);

    $find_auth_store = findAuthStore($user_id);
    if($find_auth_store){
        $money = 0; //(关闭店补)
        if($money>0){
            $repeat_money = bcmul($money,0.05,2);
            $money = bcsub($money,$repeat_money,2);
            repeatUserMoney($find_auth_store,$repeat_money,'新店扶持',$order_info['id'],10);
            $insert_data = [
                'u_id'=>$find_auth_store,
                'money'=>$money,
                'type'=>3,//门店津贴店补
                'symbol'=>1,
                'order_id'=>$order_id,//记录订单id
                'status'=>2,//直接到账
                'remark'=>'门店津贴店补'.$money,
                'created_at'=>date('Y-m-d H:i:s',time()),
                'updated_at'=>date('Y-m-d H:i:s',time())
            ];
            if(config("test.test_start")==1 && in_array($user_id,explode(',',str_replace('，', ',', config("test.test_user"))))) {
                \App\Models\TongUserProfitTest::query()->insert($insert_data);
            }else{
                \App\Models\TongUserProfit::query()->insert($insert_data);
            }
        }
    }
}

/**
 * 给领导人奖励
 * @return false
 */
function leaderReward($user_id,$order_id)
{
    $leader_id = find_leader_user($user_id);
    if($leader_id==0 || ($leader_id==$user_id)){
        return false;
    }
    if(config("reward.leadership_rewards")==0){
        return false;
    }
    //获取订单的信息
    $order_info = TongOrder::query()->where('id',$order_id)->first();
    $all_order_price = $order_info['all_order_price'];
    $money = bcmul($all_order_price,0.1);
    $leader_flow[] = [
        'u_id'=>$leader_id,
        'order_id'=>$order_id,
        'money'=>$money,
        'type'=>6,//订单的领导人奖励
        'symbol'=>1,//类型收入
        'status'=>2, //状态待到账
        'remark'=>'领导人活动奖励到账'.$money,
        'created_at'=>date('Y-m-d H:i:s',time()),
        'updated_at'=>date('Y-m-d H:i:s',time()),
    ];
    if(config("test.test_start")==1 && in_array($user_id,explode(',',str_replace('，', ',', config("test.test_user"))))){
        \App\Models\TongUserProfitTest::query()->insert($leader_flow);
    }else{
        \App\Models\TongUserProfit::query()->insert($leader_flow);
    }


}

/**
 * 给直推人的活动奖励
 * @param $user_id
 * @param $order_id
 * @return void
 */
function shareReward($user_id,$order_id,$money){
    $user_info = TongUser::query()->where('id',$user_id)->first();
    if($user_info['p_id']==0){
        return false;
    }
    if(config("reward.share_rewards")==0){
        return false;
    }

    $share_flow[] = [
        'u_id'=>$user_info['p_id'],
        'order_id'=>$order_id,
        'money'=>$money,
        'type'=>7,//活动直推奖励
        'symbol'=>1,//类型收入
        'status'=>2, //状态待到账
        'remark'=>'直推活动奖励到账'.$money,
        'created_at'=>date('Y-m-d H:i:s',time()),
        'updated_at'=>date('Y-m-d H:i:s',time()),
    ];
    if(config("test.test_start")==1 && in_array($user_id,explode(',',str_replace('，', ',', config("test.test_user"))))){
        \App\Models\TongUserProfitTest::query()->insert($share_flow);
    }else{
        \App\Models\TongUserProfit::query()->insert($share_flow);
    }

}




